2 ms·
I'm not quite sure how the fetchall() return type looks, but couldn't you just for name, age, title in cursor.fetchall(): print(name, age, title)
by wodenokoto 2y ago
I'm not quite sure how the fetchall() return type looks, but couldn't you just
for name, age, title in cursor.fetchall():
print(name, age, title)
Ofcourse you have to come up with different variable names, but it still seems more elegant to just unpack.
- Joker_vD 2y agoHonestly, the "proper" way should be passing something like this def namedtuple_factory(cursor, row): fields = [column[0] for column in cursor.description] cls = namedtuple("Row", fields) return cls._make(row) to the fetchall(), to automatically keep the names in sync with those in the SQL query string.
- ttyprintk 2y agoImplemented as conn.row_factory = sqlite3.Row
- sgarland 2y agoBy default, List[Tuple]]. You could do a list comp over the fetchall(), but at that point there’s already some magic happening, so why not make it explicit?