Source: http://blog.csdn.net/zgl_dm/article/details/8710371
The default MySQLdb returns a tuple, which is not friendly to the user and is not conducive to maintenance
Here's how it's resolved
ImportMySQLdbImportMysqldb.cursorsconn=MySQLdb.connect (Host='localhost', user ='Root', passwd="', db ='Test', compress = 1, Cursorclass= MySQLdb.cursors.DictCursor, charset='UTF8')//<-Importantcursor=conn.cursor () cursor.execute ("SELECT name, txt from table") Rows=Cursor.fetchall () cursor.close () Conn.close ()
For row in rows:
Print row [' name '], row [' txt '] # bingo!
# another (even better) is:
conn =MySQLdb. Connect (Host='localhost', user ='Root', passwd="', db ='Test', compress = 1) Cursor= Conn.cursor (Cursorclass =MySQLdb.cursors.DictCursor)# ...#Results by field namecursor =conn.cursor ()# ...#... results by field number
Note: If the Pymysql and Mysql-python libraries are installed at the same time, it will cause Mysql-python to fail, only pymsql available
"MySQL" MySQLdb Return dictionary method