pymysql模块学习之获取结果

起步

#!/usr/bin/python3
# -*- coding: utf-8 -*-
"""pymysql查询

"""

import pymysql

conn = pymysql.connect(
    user='root',
    password='',
    host='localhost',
    port=3306,
    charset='utf8mb4',
    database='hardy2_db',
)

# 游标类型
cursor = conn.cursor(cursor=None)
# cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)

sql = 'select id, age, name as nickname from pymysql_tb;'
affected = cursor.execute(query=sql)
# 拿到查询到的记录数
# print(affected)  # int, 返回受影响的行数数目

获取结果

fetchone

cursor.fetchone(),每次获取一条记录

情况一

  • 游标类型为 None,
    • 有结果时, 返回的是一个一个的元组形式
    • 无结果时, 返回 None
while 1:
    row = cursor.fetchone()
    if row is not None:
        print(f'row: {row}')
        # =========================
        # row: (5, 23, '林海峰老师')
        # row: (6, 24, 'TaiBai')
        # =========================
        continue
    break

情况二

  • 游标类型为 DictCursor 时,
    • 有结果时, 返回的是一个一个的字典形式
    • 无结果时, 返回 None
while 1:
    row = cursor.fetchone()
    if row is not None:
        print(f'row: {row}')
        # =================================================
        # row: {'id': 5, 'age': 23, 'nickname': '林海峰老师'}
        # row: {'id': 6, 'age': 24, 'nickname': 'TaiBai'}
        # =================================================
        continue
    break

情况三

如果一开始就没有数据, 不管游标类型, 都返回 None (我这写的可不是废话嘛)

fetchmany

cursor.fetchmany(size=None),每次获取指定数目的记录,默认一条

情况一

  • 游标类型为 None
    • 有结果时, 返回的是((), (), ...)形式
      • 未指定 size, 默认为 None, 返回一个
      • 指定的 size 小于结果集 size, 则取指定的 size条记录
      • 指定的 size 大于结果集 size, 则最大限度取
    • 无结果时, 返回的是()
print(cursor.fetchmany(size=None))

print(cursor.fetchmany(size=1))

print(cursor.fetchmany(size=1000))

情况二

  • 游标类型为 DictDefault,
    • 有结果时, 返回的是 [{}, {}, ...] 形式
    • 未指定 size, 默认为 None, 返回一个
    • 指定的 size 小于 结果集 size, 则取指定的 size 条记录
    • 指定的 size 大于 结果集 size, 则最大限度取
    • 无结果时, 返回的是 []
print(cursor.fetchmany(size=None))

print(cursor.fetchmany(size=1))

print(cursor.fetchmany(size=1000))

情况三

如果一开始结果集就没有数据, 不管游标的类型, 都是返回 ()

fetchall

cursor.fetchall(),获取全部数据

情况一

  • 游标类型为 None
    • 有结果时, 返回的 ((), (), ...) 形式
    • 无结果时, 返回的是 ()
print(cursor.fetchall())

print(cursor.fetchall())

情况二

  • 游标类型为 DictDefault
    • 有结果时, 返加的 [{}, {}, ...] 形式
    • 无结果时, 返回的是 []
print(cursor.fetchall())

print(cursor.fetchall())

情况三

如果一开始结果集就没有数据, 不管游标的类型, 都是返回 ()

# ================================================================
# fetchone,不管游标的类型,只要没数据了,就返回None
# fetchmany和fetchall,不管游标的类型,只要一开始没数据都是返回()空元组
# ================================================================

scroll移动

我还想拿之前的数据,怎么让游标移动呢?

cursor.scroll(value, mode='relative')

相对移动

相对当前位置移动,默认

整数往前移动,负数往后移动,数字不能超过结果集 rows 的数量,否则报错 IndexError

cursor.scroll(value=-2, mode='relative')
while 1:
    row = cursor.fetchone()
    if row is not None:
        print(f'row: {row}')
        # =========================
        # row: (5, 23, '林海峰老师')
        # row: (6, 24, 'TaiBai')
        # =========================
        continue
    break

绝对移动

在允许的范围内,指定移动到哪个位置

cursor.scroll(value=1, mode='absolute')

print(cursor.fetchone())

示例代码

#!/usr/bin/python3
# -*- coding: utf-8 -*-
"""pymysql查询

"""

import pymysql

conn = pymysql.connect(
    user='root',
    password='',
    host='localhost',
    port=3306,
    charset='utf8mb4',
    database='hardy2_db',
)

# 游标类型
# cursor = conn.cursor(cursor=None)
cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)

# 查
sql = 'select id, age, name as nickname from pymysql_tb;'
affected = cursor.execute(query=sql)
# 拿到查询到的记录数
# print(affected)  # int, 返回受影响的行数数目

# 拿到确切的结果
# 方式一: fetchone
#        每次获取一条记录
# 情况一: 游标类型为None,
#        有结果时, 返回的是一个一个的元组形式
#        无结果时, 返回None
# while 1:
#     row = cursor.fetchone()
#     if row is not None:
#         print(f'row: {row}')
#         # =========================
#         # row: (5, 23, '林海峰老师')
#         # row: (6, 24, 'TaiBai')
#         # =========================
#         continue
#     break

# 情况二: 游标类型为DictCursor时,
#        有结果时, 返回的是一个一个的字典形式
#        无结果时, 返回None
# while 1:
#     row = cursor.fetchone()
#     if row is not None:
#         print(f'row: {row}')
#         # =================================================
#         # row: {'id': 5, 'age': 23, 'nickname': '林海峰老师'}
#         # row: {'id': 6, 'age': 24, 'nickname': 'TaiBai'}
#         # =================================================
#         continue
#     break

# 情况三: 如果一开始就没有数据, 不管游标类型, 都返回None (我这写的可不是废话嘛)

# 方式二: fetchmany
# 情况一: 游标类型为None,
#        有结果时, 返回的是((), (), ...)形式
#           未指定size, 默认为None, 返回一个
#           指定的size小于结果集size, 则取指定的size条记录
#           指定的size大于结果集size, 则最大限度取
#        无结果时, 返回的是()
#
# print(cursor.fetchmany(size=None))
# print(cursor.fetchmany(size=1))
# print(cursor.fetchmany(size=1000))

# 情况二: 游标类型为DictDefault,
#        有结果时, 返回的是[{}, {}, ...]形式
#           未指定size, 默认为None, 返回一个
#           指定的size小于结果集size, 则取指定的size条记录
#           指定的size大于结果集size, 则最大限度取
#        无结果时, 返回的是[]
# print(cursor.fetchmany(size=None))
# print(cursor.fetchmany(size=1))
# print(cursor.fetchmany(size=1000))

# 情况三: 如果一开始结果集就没有数据, 不管游标的类型, 都是返回()

# 方式三: fetchall
# 情况一: 游标类型为None
#        有结果时, 返回的((), (), ...)形式
#        无结时, 返回的是()

# print(cursor.fetchall())
# print(cursor.fetchall())

# 情况二: 游标类型为DictDefault
#        有结果时, 返加的[{}, {}, ...]形式
#        无结果时, 返回的是[]

# print(cursor.fetchall())
# print(cursor.fetchall())

# 情况三: 如果一开始结果集就没有数据, 不管游标的类型, 都是返回()

# ================================================================
# fetchone,不管游标的类型,只要没数据了,就返回None
# fetchmany和fetchall,不管游标的类型,只要一开始没数据都是返回()空元组
# ================================================================

# 我还想拿之前的数据,怎么让游标移动呢?
# scroll()方法
# 方式一:相对当前位置移动,默认
# 整数往前移动,负数往后移动,数字不能超过结果集rows的数量,否则报错IndexError
# cursor.scroll(value=-2, mode='relative')
# while 1:
#     row = cursor.fetchone()
#     if row is not None:
#         print(f'row: {row}')
#         # =========================
#         # row: (5, 23, '林海峰老师')
#         # row: (6, 24, 'TaiBai')
#         # =========================
#         continue
#     break

# 方式二:相对绝对位置移动
# 在允许的范围内,指定移动到哪个位置
# cursor.scroll(value=1, mode='absolute')
# print(cursor.fetchone())

cursor.close()

conn.close()

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值