Python3使用pymysql返回字典类型

2020-03-17 11:54:18 浏览数 (3)

代码语言:javascript复制
#!/usr/bin/env python
#coding:utf-8
import  pymysql
import json


# 连接mysql
conn = pymysql.connect(
    host="192.168.3.15",
    user="root",
    password="123456",
    database="devops",
    charset="utf8"
)

# 拿到游标
  # pymysql.cursors.DictCursor :返回字典
  # 默认以元组显示
cursor = conn.cursor(pymysql.cursors.DictCursor) 

# 定义要执行的SQL语句
sql = 'select * from user where uid = 1'


# 执行sql语句
res = cursor.execute(sql)


# 打印一下返回值(查询到的数据)
  # fetchone:返回一条
  # fechall:返回全部
print(cursor.fetchone())



cursor.close()
conn.close()

0 人点赞