Python、MySQL和selectouttodictionary,使用列名作为键
我有一个MySQL表,在Python中执行SELECT语句Python、MySQL和selectouttodictionary,使用列名作为键,python,mysql,dictionary,Python,Mysql,Dictionary,我有一个MySQL表,在Python中执行SELECT语句 Python MySQLdb API中是否有任何东西可以通过游标输出一组字典,这些字典的键是列名(值是返回行中的值)?请使用字典游标: cursor = conn.cursor (MySQLdb.cursors.DictCursor) 对我来说,工作: 详细示例: import mysql.connector # pip install mysql-connector-python mydb = mysql.connector.c
Python MySQLdb API中是否有任何东西可以通过游标输出一组字典,这些字典的键是列名(值是返回行中的值)?请使用字典游标:
cursor = conn.cursor (MySQLdb.cursors.DictCursor)
对我来说,工作:
详细示例:
import mysql.connector # pip install mysql-connector-python
mydb = mysql.connector.connect(host="localhost", user="user", passwd="pass", database="dbname")
cursor = conn.cursor(dictionary=True)
sql = "SELECT * FROM `table` WHERE 1"
mycursor.execute(sql)
rows = mycursor.fetchall()
for row in rows:
row["col"]
我猜这是一个重复:也许你需要字典游标?游标=conn.cursor(MySQLdb.cursors.DictCursor)如果你愿意,让这个链接成为一个我会接受的答案。如果你没有足够的声誉投票关闭,标记答案,选择“其他”,然后输入“重复”和URL。不要发布一个只是重复链接的答案。
import mysql.connector # pip install mysql-connector-python
mydb = mysql.connector.connect(host="localhost", user="user", passwd="pass", database="dbname")
cursor = conn.cursor(dictionary=True)
sql = "SELECT * FROM `table` WHERE 1"
mycursor.execute(sql)
rows = mycursor.fetchall()
for row in rows:
row["col"]