Skip to content

Instantly share code, notes, and snippets.

@aleksei140888
Created August 6, 2022 20:24
Show Gist options
  • Save aleksei140888/331901f2f79eceeebde850b670ebaf35 to your computer and use it in GitHub Desktop.
Save aleksei140888/331901f2f79eceeebde850b670ebaf35 to your computer and use it in GitHub Desktop.
Python SQLite - select dicts - row factory lambda
import sqlite
conn = sqlite3.connect(database='my_cool_db.sqlite3')
# conn.row_factory = sqlite3.Row # not the prettiest option
conn.row_factory = lambda c, r: dict([(col[0], r[idx]) for idx, col in enumerate(c.description)])
cursor = self.conn.cursor()
result = cursor.execute("SELECT * FROM test WHERE name='myname';")
print(result)
# [{'id': 1, 'name': 'myname', 'number': 1234567890}]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment