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

python3 pymysql查询结果包含字段名

时间:2020-06-04 10:26:53      阅读:139      评论:0      收藏:0      [点我收藏+]

标签:class   import   fetch   ret   user   int   数据   exec   ctc   

python2使用MySQLdb模块进行连接mysql数据库进行操作;python3则使用pymysql模块进行连接mysql数据库进行操作;两者在语法上有稍微的差别,其中就包括查询结果包含字段名,具体例子如下:

python2:

import MySQLdb

conn = MySQLdb.connect(host=‘127.0.0.1‘,user=‘root‘,passwd=‘root‘,db=‘adu‘)
cur = conn.cursor(cursorclass=MySQLdb.cursors.DictCursor)

sql = ‘select * from test1‘
reCount = cur.execute(sql)
nRet = cur.fetchall()

cur.close()
conn.close()

print(nRet)
print(reCount)

 

pytnon3:

import pymysql

conn = pymysql.connect(host=‘127.0.0.1‘,user=‘root‘,passwd=‘root‘,db=‘adu‘)
cur = conn.cursor(cursor=pymysql.cursors.DictCursor)

sql = ‘select * from test1‘
reCount = cur.execute(sql)
nRet = cur.fetchall()

cur.close()
conn.close()

print(nRet)
print(reCount)

python3 pymysql查询结果包含字段名

标签:class   import   fetch   ret   user   int   数据   exec   ctc   

原文地址:https://www.cnblogs.com/zbcblog/p/13041736.html

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