Created
September 27, 2020 07:48
-
-
Save lucnap/4c63c94480a58806c98d0f623802200c to your computer and use it in GitHub Desktop.
Python: map field names to indices in database cursor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def generatefieldmap(cursor): | |
""" Given a DB API 2.0 cursor object that has been executed, returns | |
a dictionary that maps each field name to a column index; 0 and up. """ | |
results = {} | |
column = 0 | |
for d in cursor.description: | |
results[d[0]] = column | |
column = column + 1 | |
return results | |
# usage | |
fmap = generatefieldmap(cur); | |
print(row[fmap['nome']]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment