码迷,mamicode.com
首页 > 数据库 > 详细

pymysql 读取数据库没有字段

时间:2018-09-15 00:42:27      阅读:522      评论:0      收藏:0      [点我收藏+]

标签:try   语句   列表   aaa   limit   rip   lis   from   取数   

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

标签:try   语句   列表   aaa   limit   rip   lis   from   取数   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!