Skip to content

Instantly share code, notes, and snippets.

@lucnap
Created September 27, 2020 07:48
Show Gist options
  • Save lucnap/4c63c94480a58806c98d0f623802200c to your computer and use it in GitHub Desktop.
Save lucnap/4c63c94480a58806c98d0f623802200c to your computer and use it in GitHub Desktop.
Python: map field names to indices in database cursor
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