MySQL Connector/Python 查询如何返回字段名
请问一下,MySQL Connector/Python 查询如何返回字段名,现在每一行都是以数字的索引。
我的代码如下:
import mysql.connector
cnx = mysql.connector.connect(user='root',password='sz123',host='192.168.0.77',port='3306', database='dams')
cursor = cnx.cursor()
query = "select hotel0,hotel1 from cd_hotel limit 2"
cursor.execute(query)
for hotelrs in cursor:
print(hotelrs)
cnx = mysql.connector.connect(user='root',password='sz123',host='192.168.0.77',port='3306', database='dams', cursorclass=MySQLdb.cursors.DictCursor)
cursor = cnx.cursor()
...
for field_desc in cursor.description:
print field_desc[0]
import mysql.connector
cnx = mysql.connector.connect(user='root',password='sz123',host='192.168.0.77',port='3306', database='dams')
cursor = cnx.cursor(cursorclass = MySQLdb.cursors.DictCursor)//注意这里:cursorclass = MySQLdb.cursors.DictCursor
query = "select hotel0,hotel1 from cd_hotel limit 2"
cursor.execute(query)
for hotelrs in cursor:
print(hotelrs)