Python使用pymysql返回字典类型的数据

import pymysql
import time

# 数据库
db = ""
cur = ""
# 现在年月日
today = time.strftime("%Y-%m-%d", time.localtime())
try:
  # 数据库配置
  config = {
    "host": "124.71.18.23", 
    "port": 3306, 
    "user": "root", 
    "password": "Master@test", 
    "db": 'scrapy', 
    "charset": "utf8mb4"
  }
  db = pymysql.connect(**config)
  # 游标
  cur = db.cursor()
except:
  print("连接数据库失败")
  exit(-1)


# 查询今天之后的所有天气数据
def get_day_list(date):
  sql_weather_days = "SELECT*FROM weather WHERE date>='" + date + "'"
  cur.execute(sql_weather_days)
  dayList = cur.fetchall()
  print(dayList)
  pass


get_day_list(today)

结果

(
(448, '周六 06', '2021-02-06', '--', '5°', 'Mostly Clear Night', datetime.datetime(2021, 2, 6, 23, 0, 11)), 
(449, '周日 07', '2021-02-07', '19°', '7°', 'Mostly Sunny', datetime.datetime(2021, 2, 6, 23, 0, 11)), 
(450, '周一 08', '2021-02-08', '9°', '3°', 'Mostly Cloudy', datetime.datetime(2021, 2, 6, 23, 0, 11)), 
(451, '周二 09', '2021-02-09', '12°', '7°', 'Partly Cloudy', datetime.datetime(2021, 2, 6, 23, 0, 11)), 
(452, '周三 10', '2021-02-10', '15°', '10°', 'Cloudy', datetime.datetime(2021, 2, 6, 23, 0, 11))
)

这样,读取起来不是很方便,那么如何返回字典的数据格式的数据呢?

在默认情况下cursor方法返回的是BaseCursor类型对象,BaseCursor类型对象在执行查询后每条记录的结果以列表(list)表示。如果要返回字典(dict)表示的记录,就要设置cursorclass参数为MySQLdb.cursors.DictCursor类

import pymysql
import time

# 数据库
db = ""
cur = ""
# 现在年月日
today = time.strftime("%Y-%m-%d", time.localtime())
try:
  # 数据库配置
  config = {
    "host": "124.71.18.23", 
    "port": 3306, 
    "user": "root", 
    "password": "Master@test", 
    "db": 'scrapy', 
    "charset": "utf8mb4",
    "cursorclass": pymysql.cursors.DictCursor
  }
  db = pymysql.connect(**config)
  # 游标
  cur = db.cursor()
except:
  print("连接数据库失败")
  exit(-1)


# 查询今天之后的所有天气数据
def get_day_list(date):
  sql_weather_days = "SELECT*FROM weather WHERE date>='" + date + "'"
  cur.execute(sql_weather_days)
  dayList = cur.fetchall()
  print(dayList)
  pass


get_day_list(today)

结果

[
{'id': 448, 'name': '周六 06', 'date': '2021-02-06', 'max': '--', 'min': '5°', 'status': 'Mostly Clear Night', 'create_time': datetime.datetime(2021, 2, 6, 23, 0, 11)}, 
{'id': 449, 'name': '周日 07', 'date': '2021-02-07', 'max': '19°', 'min': '7°', 'status': 'Mostly Sunny', 'create_time': datetime.datetime(2021, 2, 6, 23, 0, 11)}, 
{'id': 450, 'name': '周一 08', 'date': '2021-02-08', 'max': '9°', 'min': '3°', 'status': 'Mostly Cloudy', 'create_time': datetime.datetime(2021, 2, 6, 23, 0, 11)}, 
{'id': 451, 'name': '周二 09', 'date': '2021-02-09', 'max': '12°', 'min': '7°', 'status': 'Partly Cloudy', 'create_time': datetime.datetime(2021, 2, 6, 23, 0, 11)}, 
{'id': 452, 'name': '周三 10', 'date': '2021-02-10', 'max': '15°', 'min': '10°', 'status': 'Cloudy', 'create_time': datetime.datetime(2021, 2, 6, 23, 0, 11)}
]
  • 8
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
pymysql中,游标(cursor)可以分为三种类型:普通游标(cursor),字典游标(DictCursor)和SSCursor。 1. 普通游标(cursor):返回数据类型是元组(tuple),使用方法如下: ```python import pymysql # 连接数据库 conn = pymysql.connect(host='localhost', port=3306, user='root', password='', db='test') # 创建游标 cursor = conn.cursor() # 执行SQL语句 sql = "SELECT * FROM user" cursor.execute(sql) # 获取所有数据 result = cursor.fetchall() print(result) # 关闭游标和连接 cursor.close() conn.close() ``` 2. 字典游标(DictCursor):返回数据类型字典(dict),使用方法如下: ```python import pymysql # 连接数据库 conn = pymysql.connect(host='localhost', port=3306, user='root', password='', db='test') # 创建字典游标 cursor = conn.cursor(pymysql.cursors.DictCursor) # 执行SQL语句 sql = "SELECT * FROM user" cursor.execute(sql) # 获取所有数据 result = cursor.fetchall() print(result) # 关闭游标和连接 cursor.close() conn.close() ``` 3. SSCursor:在处理大量数据时,可以使用SSCursor,返回数据类型同普通游标,但是SSCursor不会一次性将所有数据都读取进内存,而是在需要时才获取数据使用方法如下: ```python import pymysql # 连接数据库 conn = pymysql.connect(host='localhost', port=3306, user='root', password='', db='test') # 创建SSCursor cursor = conn.cursor(pymysql.cursors.SSCursor) # 执行SQL语句 sql = "SELECT * FROM user" cursor.execute(sql) # 获取数据 while True: result = cursor.fetchone() if not result: break print(result) # 关闭游标和连接 cursor.close() conn.close() ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值