Python Forum

Full Version: MySql Cursor field names.
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
HI folks.

I've seen this somewhere, but I can't find it...

I can:
cursor.execute("blah and pharp...
for row in cursor:
print row[0]
print row[1]

but how do I get to use the field names? I think it's something to do with a dictionary?

print row{'field1']
print row['field2']

Thanks.
Probably. I think you can use whatever aliases you're using in the query as dict keys. When in doubt, inspect!
for row in cursor:
    print(row)
    print(dir(row))