Documentation Home
MySQL Connector/Python Developer Guide
Related Documentation Download this Manual
PDF (US Ltr) - 0.7Mb
PDF (A4) - 0.7Mb


10.6.3 cursor.MySQLCursorDict Class

The MySQLCursorDict class inherits from MySQLCursor. This class is available as of Connector/Python 2.0.0.

A MySQLCursorDict cursor returns each row as a dictionary. The keys for each dictionary object are the column names of the MySQL result.

Example:

Press CTRL+C to copy
cnx = mysql.connector.connect(database='world') cursor = cnx.cursor(dictionary=True) cursor.execute("SELECT * FROM country WHERE Continent = 'Europe'") print("Countries in Europe:") for row in cursor: print("* {Name}".format(Name=row['Name']

The preceding code produces output like this:

Press CTRL+C to copy
Countries in Europe: * Albania * Andorra * Austria * Belgium * Bulgaria ...

It may be convenient to pass the dictionary to format() as follows:

Press CTRL+C to copy
cursor.execute("SELECT Name, Population FROM country WHERE Continent = 'Europe'") print("Countries in Europe with population:") for row in cursor: print("* {Name}: {Population}".format(**row))