python查询mysql以字典返回

Posted brady-wang

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python查询mysql以字典返回相关的知识,希望对你有一定的参考价值。

# *_*coding:utf-8 *_*
import pymysql

conn = pymysql.connect(host=‘192.168.33.10‘, user=‘root‘, passwd=‘root‘, db=‘spider‘, charset=‘utf8‘)
cur = conn.cursor(cursor=pymysql.cursors.DictCursor)

sql = "select * from topic order by id asc "
cur.execute(sql)  # 返回受影响的行数
res = cur.fetchall()  # 返回数据,返回的是tuple类型

for item in res:
    print(item[‘id‘])
cur.close()
conn.close()

  

以上是关于python查询mysql以字典返回的主要内容,如果未能解决你的问题,请参考以下文章