pymysql 讀取數據庫沒有字段
阿新 • • 發佈:2018-09-15
select result app mysq 查詢 exec sele aaa con
import pymysql
# 打開數據庫連接
db = pymysql.connect("localhost", "root", "root", "test")
# 使用cursor()方法獲取操作遊標
cursor = db.cursor()
# SQL 查詢語句
sql = "SELECT * FROM aaa limit 2"
try:
# 執行SQL語句
b=cursor.execute(sql)
#print(b)記錄條數
#print(cursor.description)#獲取字段列表
result = cursor.fetchall()
lists=[]
t=0
for x in result:
i=0
onelist={}
for field in cursor.description:
onelist[field[0]]=x[i]
i=i+1
lists.append(onelist)
for q in lists:
print(q[‘id‘])
print(q[‘data‘])
except:
print ("Error: unable to fetch data")
# 關閉數據庫連接
db.close()
pymysql 讀取數據庫沒有字段