MySQL Connector/Python Developer Guide / ... / Connector/Python API Reference / cursor.MySQLCursor Class / MySQLCursor.column_names Property
Syntax:
sequence = cursor.column_namesThis read-only property returns the column names of a result set as sequence of Unicode strings.
The following example shows how to create a dictionary from a tuple containing data with keys usingcolumn_names:
cursor.execute("SELECT last_name, first_name, hire_date " "FROM employees WHERE emp_no = %s", (123,))row = dict(zip(cursor.column_names, cursor.fetchone()))print("{last_name}, {first_name}: {hire_date}".format(row))Alternatively, as of Connector/Python 2.0.0, you can fetch rows as dictionaries directly; seeSection 10.6.3, “cursor.MySQLCursorDict Class”.