5.9.6.4 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 is the column names of the MySQL result.
Example:
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:
Countries in europe:* albania* andorra* austria* belgium* Bulgaria ...
It is convenient to pass the dictionary to as format()
follows:
Cursor.execute ("Select Name, Population from country WHERE continent = ' Europe '") print ("Countries on Europe with Populatio N: ") for row in cursor: print (" * {Name}: {Population} ". Format (**row))
PREVHOME up NEXTUser CommentsPosted by Blair Gemmer on December If you want the stored procedures, please use the This format:
Cursor.callproc (Stored_procedure_name, args)
result = []
For Recordset in Cursor.stored_results ():
For row in Recordset:
Result.append (dict (Zip (recordset.column_names,row))) ============== 10.5.11 Mysqlcursor.column_names Property
Syntax:
sequence = Cursor.column_names
This 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 using column_names
:
Cursor.execute ("Select Last_Name, first_name, hire_date" "from employees WHERE Emp_no =%s", (123,)) row = Dict (Zip (cu Rsor.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; See section 10.6.4, "cursor." Mysqlcursordict Class ".
Cursor. Mysqlcursordict Class