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)