How to efficiently get rows back as dictionaries rather than tuples?

1 view
Skip to first unread message

Scott Chapman

unread,
Jan 9, 2026, 8:29:39 PM (5 days ago) Jan 9
to python...@googlegroups.com
... Using the column name as they key, how do I tell APSW/SQLite to give me data as tuples in the most efficient manner?

connection.setrowtrace(callback_function) will run the callback for every row. Is there a more efficient method?

Thanks,
Scott

Roger Binns

unread,
Jan 10, 2026, 10:07:15 AM (5 days ago) Jan 10
to Python-SQLite group
> ... Using the column name as they key, how do I tell APSW/SQLite to
> give me data as tuples in the most efficient manner?

The most efficient is to accept back unnamed tuples. It does duplicate, but a pattern like this works:

for product, price, sku in db.execute("SELECT product, price, sku FROM ....")

> connection.setrowtrace(callback_function) will run the callback for
> every row. Is there a more efficient method?

If you want names, then some code somewhere has to run for every row. I would advise using dataclasses. The next release defaults slots to True for dataclasses saving some memory.

https://rogerbinns.github.io/apsw/ext.html#accessing-result-rows-by-column-name


In summary, the least amount of CPU cycles is bare tuples, the most ergonomic and efficient for names is to use dataclasses

Roger
Reply all
Reply to author
Forward
0 new messages