SK9*_*SK9 12 python mysql dictionary
我有一个MySQL表,我在Python中执行SELECT语句.
Python MySQLdb API中是否有任何东西可以通过游标输出一个字典数组,其键是列名(值是返回行中的值)?
var*_*ela 27
请使用字典光标:
cursor = conn.cursor (MySQLdb.cursors.DictCursor)
Run Code Online (Sandbox Code Playgroud)
DaW*_*aWe 18
对我来说,这有效:
cursor = conn.cursor(dictionary=True)
Run Code Online (Sandbox Code Playgroud)
详细示例:
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"]
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
10189 次 |
最近记录: |