zoukankan      html  css  js  c++  java
  • pymysql基本的使用方法

    1、导入模块+创建连接

    import pymysql
    
    
    # 1、通过python去连接数据库
    conn = pymysql.connect(host="127.0.0.1",port=3306,user="root",passwd="admin123.",db="test_python_1",charset="utf8")
    

      

    2、创建游标,通过游标就可以执行sql命令

    # 2、创建游标
    test_cursor = conn.cursor()
    

      

    3、通过游标执行sql命令,插入一条数据

    插入前的表的内容

    执行命令

    # 3、执行一个插入的命令
    r = test_cursor.execute("insert into test(name,age) VALUES ('999',23)")
    

      

    这里的r的值就受影响的行的数目

    这里要注意,如果执行修改相关的命令,则必须要commit才能生效,commit操作由数据库连接的对象的操作,而不是通过游标去执行

    # 4、提交数据
    conn.commit()
    

      

    查看表中的内容,已经多了一条数据

    4、python的数据交互,由pymysql内部做参数传递

    inp = input("请输入姓名:")
    r = test_cursor.execute("insert into test(name,age) VALUES (%s,34)",inp)
    # 这里可以用字符串拼接,也可以使用pymysql内部给我们做字符串拼接
    # 字符串拼接是可以的,但是这里禁止操作的,这里会引起sql注入,所以不能这么使用
    
    
    print(r)
    # 这里的r的意思受影响的行数
    

    我们看表中的数据已经更新

    上面只传递了一个参数,如果有多个参数,该怎么传递呢?这里要传递一个元组进去

    inp = input("请输入姓名:")
    inp_age = input("请输入年龄:")
    r = test_cursor.execute("insert into test(name,age) VALUES (%s,%s)",(inp,inp_age))
    

      

    我们看到数据库的表中的数据已经更新

     上面的例子我们只插入了一条数据,那么如果我们想一次插入多条数据该怎么弄呢?解决办法看下面的例子,要用excutemany命令来执行,然后通过列表或者元组把参数传递进去

    user_info_list = [
        ("cui1",23),
        ("cui2",24),
        ("cui3",25)
    ]
    
    r = test_cursor.executemany("insert into test(name,age) values (%s,%s)",user_info_list)
    print(r)
    

      

    我们可以看到数据库的表中已经有数据更新

     4、通过pymysql更新数据库中的内容

    r = test_cursor.execute("update test set name = %s where age = 25","张国军")
    
    print(r)
    

      

    我们可以看到数据库的表中的内容已经更新

    5、通过pymysql删除数据库表中的内容

    r = test_cursor.execute("delete from test where age = 25")
    print(r)
    

      

    我们可以看到表中的数据已经更新

    6、通过pymysql查询数据,如果是查数据,则不需要commit

    r = test_cursor.execute("select * from test")
    print(test_cursor.fetchone())
    print(test_cursor.fetchone())
    print(test_cursor.fetchmany(2))
    print(test_cursor.fetchall())
    

      

    我们注意数据库中的内容

    我们看pymysql得到的结果

    我们可以看到上面有一个指针的概念,第一条数据被取出来后,第二次在取数据,则就从第二条数据开始取,那么我们有没有办法移动指针呢?当然有,我们看下面的例子

    7、移动指针

    绝对的方式移动指针

    r = test_cursor.execute("select * from test")
    print(test_cursor.fetchone())
    test_cursor.scroll(0,mode = "absolute")
    # 这里的意思,absolute是绝对的意思,让指针回到0的位置
    
    print(test_cursor.fetchone())
    

      

    我们可以看到2次取的数据是一样的

     相对的方式移动指针

    r = test_cursor.execute("select * from test")
    print(test_cursor.fetchone())
    # test_cursor.scroll(0,mode = "absolute")
    # 这里的意思,absolute是绝对的意思,让指针回到0的位置
    
    test_cursor.scroll(2,mode="relative")
    # 这里的意思,relative是相对的意思,相当当前的位置向下移动2个位置,这里如果是负数的话,是向上移动,如果是正数,则向下移动
    

      

    我们注意数据库中的表的顺序

    我们看下pymysql执行的结果

  • 相关阅读:
    HIVE Group by、join、distinct等实现原理
    【1.3】shell基础——调试shell(sh -x)
    sql server无法显示请求的对话框,检索数据失败
    sql server索引操作
    sql server中的alter
    tempdb无法收缩。使用 DBCC FREESYSTEMCACHE 解决
    在从该备份集进行读取时,RESTORE 检测到在数据库 "CISDB" 中的页(0:0)上存在错误。系统断定检查已失败
    【1.2】shell基础——stty erase解决按backspace出现^H的情况
    【1.1】shell基本实践——密码输入三次错误则结束
    (5.3.7)数据库迁移——sql server备份文件的加密解密
  • 原文地址:https://www.cnblogs.com/bainianminguo/p/8428588.html
Copyright © 2011-2022 走看看