Sqlite and Python — return a dictionary using fetchone()?

There is actually an option for this in sqlite3. Change the row_factory member of the connection object to sqlite3.Row:

conn = sqlite3.connect('db', row_factory=sqlite3.Row)

or

conn.row_factory = sqlite3.Row

This will allow you to access row elements by name–dictionary-style–or by index. This is much more efficient than creating your own work-around.

Leave a Comment