cursor = conn.cursor(MySQLdb.cursors.DictCursor)
cursor.execute("SELECT name, category FROM animal")
result_set = cursor.fetchall()
for row in result_set:
print "%s, %s" % (row["name"], row["category"])
//column names
num_fields = len(cursor.description)
field_names = [i[0] for i in cursor.description]
選我正解
利用cursor
物件的description
屬性
cursor.execute("SELECT * FROM TABLE_NAME")
field_name = [des[0] for des in cursor.description]
在pymysql中有範例
import pymysql
conn = pymysql.connect(host="localhost", port=3306, user="root", passwd="", db="mysql")
cur = conn.cursor()
cur.execute("SELECT Host,User FROM user")
print(cur.description)
print()
for row in cur:
print(row)
cur.close()
conn.close()
使用cur.description應該就可以列出