首页 > 数据库技术 > 详细

pymysql 读取数据库没有字段

时间:2018-09-15 00:17:57      阅读:528      评论:0      收藏:0      [点我收藏+]

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 读取数据库没有字段

原文:https://www.cnblogs.com/weiyiyong/p/9649544.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!