Created
August 6, 2022 20:24
-
-
Save aleksei140888/331901f2f79eceeebde850b670ebaf35 to your computer and use it in GitHub Desktop.
Python SQLite - select dicts - row factory lambda
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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