zoukankan      html  css  js  c++  java
  • Python的pymysql模块

    PyMySQL是在Python3.x版本中用于连接MySQL服务器的一个库,Python2中则使用MySQLDB。

    1.基本语法

    # 导入pymysql模块
    import pymysql
    # 连接database
    conn = pymysql.connect(host=“你的数据库地址”, user=“用户名”,password=“密码”,database=“数据库名”,charset=“utf8”)
    # 得到一个可以执行SQL语句的光标对象
    cursor = conn.cursor()
    # 定义要执行的SQL语句
    sql = 
    # 执行SQL语句
    cursor.execute(sql)
    #批量执行
    cursor.executemany(sql)
    # 提交事务 conn.commit()
    #查
    cursor.lastrowid()
    cursor.fetchone()
    cursor.fetchall()
    cursor.fetchmany()
    # 关闭光标对象 cursor.close() # 关闭数据库连接 conn.close()

    2.增

    # 导入pymysql模块
    import pymysql
    # 连接database
    conn = pymysql.connect(host=“你的数据库地址”, user=“用户名”,password=“密码”,database=“数据库名”,charset=“utf8”)
    # 得到一个可以执行SQL语句的光标对象
    cursor = conn.cursor()
    sql = "INSERT INTO USER1(name, age) VALUES (%s, %s);"
    username = "kebi"
    age = 18
    try:
        # 执行SQL语句,传参的写法避免SQL语句注入
        cursor.execute(sql, [username, age])
        # 提交事务
        conn.commit()
    except Exception as e:
        # 有异常,回滚事务
        conn.rollback()
    cursor.close()
    conn.close()

    获取插入数据的ID(关联操作时会用到)

    # 导入pymysql模块
    import pymysql
    # 连接database
    conn = pymysql.connect(host=“你的数据库地址”, user=“用户名”,password=“密码”,database=“数据库名”,charset=“utf8”)
    # 得到一个可以执行SQL语句的光标对象
    cursor = conn.cursor()
    sql = "INSERT INTO USER1(name, age) VALUES (%s, %s);"
    username = "kebi"
    age = 18
    try:
        # 执行SQL语句
        cursor.execute(sql, [username, age])
        # 提交事务
        conn.commit()
        # 提交之后,获取刚插入的数据的ID
        last_id = cursor.lastrowid
    except Exception as e:
        # 有异常,回滚事务
        conn.rollback()
    cursor.close()
    conn.close()

    批量执行

    # 导入pymysql模块
    import pymysql
    # 连接database
    conn = pymysql.connect(host=“你的数据库地址”, user=“用户名”,password=“密码”,database=“数据库名”,charset=“utf8”)
    # 得到一个可以执行SQL语句的光标对象
    cursor = conn.cursor()
    sql = "INSERT INTO USER1(name, age) VALUES (%s, %s);"
    data = [("kebi", 18), ("maoxian", 20), ("xiaoniao", 21)]
    try:
        # 批量执行多条插入SQL语句
        cursor.executemany(sql, data)
        # 提交事务
        conn.commit()
    except Exception as e:
        # 有异常,回滚事务
        conn.rollback()
    cursor.close()
    conn.close()

    3.删

    # 导入pymysql模块
    import pymysql
    # 连接database
    conn = pymysql.connect(host=“你的数据库地址”, user=“用户名”,password=“密码”,database=“数据库名”,charset=“utf8”)
    # 得到一个可以执行SQL语句的光标对象
    cursor = conn.cursor()
    sql = "DELETE FROM USER1 WHERE id=%s;"
    try:
        cursor.execute(sql, [4])
        # 提交事务
        conn.commit()
    except Exception as e:
        # 有异常,回滚事务
        conn.rollback()
    cursor.close()
    conn.close()

    4.改

    # 导入pymysql模块
    import pymysql
    # 连接database
    conn = pymysql.connect(host=“你的数据库地址”, user=“用户名”,password=“密码”,database=“数据库名”,charset=“utf8”)
    # 得到一个可以执行SQL语句的光标对象
    cursor = conn.cursor()
    # 修改数据的SQL语句
    sql = "UPDATE USER1 SET age=%s WHERE name=%s;"
    username = "kebi"
    age = 80
    try:
        # 执行SQL语句
        cursor.execute(sql, [age, username])
        # 提交事务
        conn.commit()
    except Exception as e:
        # 有异常,回滚事务
        conn.rollback()
    cursor.close()
    conn.close()

    5.查

    # 导入pymysql模块
    import pymysql
    # 连接database
    conn = pymysql.connect(host=“你的数据库地址”, user=“用户名”,password=“密码”,database=“数据库名”,charset=“utf8”)
    # 得到一个可以执行SQL语句的光标对象
    cursor = conn.cursor()
    # 查询数据的SQL语句
    sql = "SELECT id,name,age from USER1 WHERE id=1;"
    # 执行SQL语句
    cursor.execute(sql)
    # 获取单条查询数据
    ret = cursor.fetchone()
    #获取指定数量的数据
    ret2=cursor.fetctmany(3)
    #获取所有
    ret3=cursor.fetchall()
    cursor.close()
    conn.close()
    # 打印下查询结果
    print(ret)

    6.其它

    # 光标按绝对位置移动1
    cursor.scroll(1, mode="absolute")
    # 光标按照相对位置(当前位置)移动1
    cursor.scroll(1, mode="relative")
  • 相关阅读:
    剑指offer23-二叉搜索树的后序遍历序列
    剑指offer24-二叉树中和为某一值的路径
    剑指offer-复杂链表的复制
    剑指offer-二叉搜索树与双向链表
    剑指offer-字符串的排序
    剑指offer-数组中出现次数超过一半的数字
    剑指offer-最小的k个数
    c++中参数加&与不加的区别
    第九届蓝桥杯(A组填空题)
    基于优先级的多道程序系统作业调度——基于优先级
  • 原文地址:https://www.cnblogs.com/yangmingxianshen/p/8258880.html
Copyright © 2011-2022 走看看