PYMSQL笔记

本文详细介绍了如何使用PyMySQL库进行MySQL数据库的连接、查询、增删改查操作,包括游标的使用、数据提交以及数据查询方法如fetchone、fetchmany和fetchall。此外,还展示了游标移动的规则,帮助开发者更好地理解和掌握Python数据库操作。
摘要由CSDN通过智能技术生成

PYMSQL操作指南

一.安装
pip3 install PyMySQL
二.连接查询数据库
import pymysql
# 连接数据库
conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', password='root', db='mydb', charset='utf8')
print(conn)

# 创建游标
cursor = conn.cursor()

# sql语句,已知数据库存在student表
sql = 'select * from student;'

# 执行sql语句,查询成功则返回查询记录数
records = cursor.execute(sql)
print(records)

# 关闭连接,游标和连接都要关闭
cursor.close()
conn.close()
三.增、删、改:conn.commit(),这些操作后一定要提交
  • 下列操作通用代码

    import pymysql
    # 连接数据库
    conn = pymysql.connect(
        host='localhost',
        port=3306,
        user='root',
        password='root',
        db='mydb',
        charset='utf8')
    
    # 创建游标
    cursor = conn.cursor()
    
    # 增删查改一定要提交,否则操作无效
    conn.commit()
    
    # 关闭游标
    cursor.close()
    # 关闭连接
    conn.close()
    
  • 增,插入数据

    • 插入一条数据

      # 这里把所有的匹配形式全部写成%s,不用去管mysql的定义的字段数据类型,源码中,已经将所有类型的数据转换成了字符串类型。
      sql = 'insert into student(id,name,age) values(%s,%s,%s) '
      insert_one = cursor.execute(sql,('1','万二','12'))
      print(insert_one)
      
    • 插入多条数据(在后面加many),数据通过列表形式传入

      sql = 'insert into student(id,name,age) values(%s,%s,%s)'
      insert_many = cursor.executemany(sql,
                                       [('7','李毅','22'),
                                        ('8','王二','42'),
                                        ('9','赵四','25')])
      print(insert_many)
      
  • 删,删除

    # 删除数据
    sql = 'delete from student where name = "赵四" '
    delete_records = cursor.execute(sql)
    print(delete_records)
    
  • # 修改id为2的人的年龄为99
    sql = 'update student set age = %s where id = 2'
    update_records = cursor.execute(sql,'99')
    print(update_records)
    
四.查:fetchone、fetchmany、fetchall
  • 下列操作通用格式

    import pymysql
    # 连接数据库
    conn = pymysql.connect(
        host='localhost',
        port=3306,
        user='root',
        password='root',
        db='mydb',
        charset='utf8')
    
    # 创建游标
    cursor = conn.cursor()
    # 查询所有记录
    sql = 'select * from student'
    cursor.execute(sql)
    
    # 关闭游标
    cursor.close()
    # 关闭连接
    conn.close()
    
  • fetchone查看一条符合条件的数据,可以连续使用,查询的是上一个fetchone的后面一条

    # 查询第一行数据
    first = cursor.fetchone()
    print(first)
    
    # 查询第二行数据
    second = cursor.fetchone()
    print(second)
    
  • fetchmany:获取指定的条数数据

    # 只要3条符合条件的
    raws = cursor.fetchmany(3)
    print(raws)
    
  • fetchall():查询所有符合条件的数据

    raws = cursor.fetchall()
    print(raws)
    
五.游标移动
  • 下面通用代码格式

    import pymysql
    # 连接数据库
    conn = pymysql.connect(
        host='localhost',
        port=3306,
        user='root',
        password='root',
        db='mydb',
        charset='utf8')
    
    # 创建游标
    cursor = conn.cursor()
    # 查询所有记录
    sql = 'select * from student'
    cursor.execute(sql)
    
    # 关闭游标
    cursor.close()
    # 关闭连接
    conn.close()
    
  • 游标移动规则

  • cursor.scroll(num,mode=‘relative|absolute’) 当mode=absolute时,num不能小于0

cursor.scroll(1,mode='relative')  # 相对当前位置移动
cursor.scroll(2,mode='absolute') # 相对绝对位置移动
  • 代码实操

    # 查询第一行数据
    a = cursor.fetchone()   # 取出第一行数据0号位置
    print("a:",a) # (1, '张三', 20)   --->游标指向1
    
    # 相对当前位置移动一位往后移动一位
    cursor.scroll(1,mode='relative')  # ----> 游标指向2
    b = cursor.fetchone()
    print('b:',b) # (3, '李四', 25)  -----> 游标指向3
    
    # 相对当前位置往前一位
    cursor.scroll(-1,mode='relative')  # ------> 游标指向2
    c = cursor.fetchone()
    print("c:",c) # (3, '李四', 25)   ------> 游标指向3
    
    # 相对绝对位置(0号位置,第一行)往后移动三位,设置之后,光标指向第四行位置,所以打印的结果为第四行数据
    cursor.scroll(3,mode='absolute') # ------->游标指向4
    d = cursor.fetchone()        # 取出第4行数据
    print("d:",d)             # ---------->游标指向5
    
    e = cursor.fetchone() # 打印第5行数据
    print('e:',e)   # --------->游标指向6
    
    f = cursor.fetchall() # 取出第6行数据以后所有的数据
    print('f:',f)
    
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小帆芽芽

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值