pymysql 读取数据库没有字段

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()

猜你喜欢

转载自www.cnblogs.com/weiyiyong/p/9649544.html