Python Forum
MySql Cursor field names. - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: MySql Cursor field names. (/thread-13590.html)



MySql Cursor field names. - MuntyScruntfundle - Oct-22-2018

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.


RE: MySql Cursor field names. - nilamo - Oct-22-2018

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))