MySQLdb default query results are returned tuple, the output time is not very convenient, you must follow the 0,1 so read, inadvertently found on the internet a simple way to modify, is to pass a cursors. Dictcursor on the line.
Default program:
MySQLdb default query results are returned tuple, the output time is not very convenient, you must follow the 0,1 so read, inadvertently found on the internet a simple way to modify, is to pass a cursors. Dictcursor on the line. Default program:
Copy Code code as follows:
Import MySQLdb
db = MySQLdb.connect (host =´localhost´, user =´root´, passwd =´123456´, DB =´test´)
cursor = Db.cursor ()
Cursor.execute (´select * from Table´)
rs = Cursor.fetchall ()
Print RS
# returns similar to the following
# (1000L, 0L), (2000L, 0L), (3000L, 0L)
After modification:
Copy Code code as follows:
Import MySQLdb
Import Mysqldb.cursors
db = MySQLdb.connect (host =´localhost´, user =´root´, passwd =´123456´, db =´test´,cursorclass = MySQLdb.cursors.DictC Ursor)
cursor = Db.cursor ()
Cursor.execute (´select * from Table´)
rs = Cursor.fetchall ()
Print RS
# returns similar to the following
# ({' Age ': 0L, ' num ': 1000L}, {' Age ': 0L, ' num ': 2000L}, {' Age ': 0L, ' num ': 3000L}) or you can also replace the Connect and cursor sections with the following
Copy Code code as follows:
db = MySQLdb.connect (host =´localhost´, user =´root´, passwd =´123456´, DB =´test´)
cursor = conn.cursor (Cursorclass = MySQLdb.cursors.DictCursor)