Python returns the dictionary structure code when querying Mysql.

Source: Internet
Author: User

MySQLdb's default query results are all returned tuple, which is not very convenient for output. It must be read according to. If you accidentally find a simple modification method on the internet, you just need to pass a cursors. DictCursor.
Default program:
MySQLdb's default query results are all returned tuple, which is not very convenient for output. It must be read according to. If you accidentally find a simple modification method on the internet, you just need to pass a cursors. DictCursor. Default program: Copy codeThe Code is as follows: import MySQLdb
Db = MySQLdb. connect (host = 'localhost', user = 'root', passwd = '000000', db = 'test ')
Cursor = db. cursor ()
Cursor.exe cute ('select * from table ')
Rs = cursor. fetchall ()
Print rs

# The returned result is similar to the following:
# (1000L, 0L), (2000L, 0L), (3000L, 0L ))
After modification:Copy codeThe Code is as follows: import MySQLdb
Import MySQLdb. cursors
Db = MySQLdb. connect (host = 'localhost', user = 'root', passwd = '000000', db = 'test', cursorclass = MySQLdb. cursors. DictCursor)
Cursor = db. cursor ()
Cursor.exe cute ('select * from table ')
Rs = cursor. fetchall ()
Print rs

# The returned result is similar to the following:
# ({'Age': 0L, 'num': 1000L}, {'age': 0L, 'num': 2000L}, {'age': 0L, 'num': 3000L}) or you can use the following to replace the connect and cursor sections.Copy codeThe Code is as follows: db = MySQLdb. connect (host = 'localhost', user = 'root', passwd = '000000', db = 'test ')
Cursor = conn. cursor (cursorclass = MySQLdb. cursors. DictCursor)

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.