Reputation: 10097
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?
Upvotes: 10
Views: 24154
Reputation: 180162
For this reason, it is recommended to always use explicit column names when doing a SELECT
:
c.execute("SELECT color, fluffiness FROM table")
for row in c.fetchall():
print row[0] # <-- is always guaranteed to be the color value
print row[1]
Upvotes: 12
Reputation: 23346
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.
Upvotes: 13