Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I select a column by its name? (Python & SQLite3)

Tags:

python

sqlite

Currently I have the following code:

c.execute("SELECT * FROM table")
for row in c.fetchall():
    print row[0]
    print row[1]

However, I changed the structure of my table and now I have to change the index values to represent this change. Is there a way to get use column names instead?

like image 234
Sheldon Avatar asked Nov 14 '12 19:11

Sheldon


1 Answers

See Row Objects in the docs for the sqlite3 module. If you use the sqlite3.Row row_factory you'll get back an object that's slightly more powerful than the normal tuples. I imagine it has slightly higher overhead, hence not being the default behavior.

like image 140
Iguananaut Avatar answered Oct 14 '22 17:10

Iguananaut