Cursor. Mysqlcursordict Class

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.