zoukankan      html  css  js  c++  java
  • pymysql基础

    一,基本使用

    倒入模块

    import pymysql
    
    
    
    conn=pymysql.connect(
    
    host="数据库地址,本机是localhost,别的机器是ip“,
    user="用户名,一般是root",
    password="密码",
    database="数据库名",
    charset="utf8"
    
    
    
    
    )

    得到一个光标对象

    cursor=conn.cursor()

    定义要执行的语句

    sql=“”

    执行语句:

    cursor.execute(sql)

    关闭

    cursor。close()

    conn.close()

    如果想返回的是字典的形式的话

    cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)

    二,增删改查

    1,增

    cursor = conn.cursor()
    sql = "INSERT INTO a1(name, age) VALUES (%s, %s);"
    username = "二狗"
    age = 18
    # 执行SQL语句
    cursor.execute(sql, [username, age])
    # 提交事务
    conn.commit()
    cursor.close()
    conn.close()

    2,插入数据失败回滚

    # 得到一个可以执行SQL语句的光标对象
    cursor = conn.cursor()
    sql = "INSERT INTO a1(name, age) VALUES (%s, %s);"
    username = "二狗"
    age = 18
    try:
        # 执行SQL语句
        cursor.execute(sql, [username, age])
        # 提交事务
        conn.commit()
    except Exception as e:
        # 有异常,回滚事务
        conn.rollback()
    cursor.close()
    conn.close()

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

    # 得到一个可以执行SQL语句的光标对象
    cursor = conn.cursor()
    sql = "INSERT INTO a1(name, age) VALUES (%s, %s);"
    username = "二狗"
    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()

    4批量执行

    # 得到一个可以执行SQL语句的光标对象
    cursor = conn.cursor()
    sql = "INSERT INTO a1(name, age) VALUES (%s, %s);"
    data = [("大狗", 18), ("二狗", 20), ("三狗", 21)]
    try:
        # 批量执行多条插入SQL语句
        cursor.executemany(sql, data)
        # 提交事务
        conn.commit()
    except Exception as e:
        # 有异常,回滚事务
        conn.rollback()
    cursor.close()
    conn.close()

    5,删

    # 得到一个可以执行SQL语句的光标对象
    cursor = conn.cursor()
    sql = "DELETE FROM a1 WHERE id=%s;"
    try:
        cursor.execute(sql, [4])
        # 提交事务
        conn.commit()
    except Exception as e:
        # 有异常,回滚事务
        conn.rollback()
    cursor.close()
    conn.close()

    6,改

    # 得到一个可以执行SQL语句的光标对象
    cursor = conn.cursor()
    # 修改数据的SQL语句
    sql = "UPDATE a1 SET age=%s WHERE name=%s;"
    username = "二狗"
    age = 80
    try:
        # 执行SQL语句
        cursor.execute(sql, [age, username])
        # 提交事务
        conn.commit()
    except Exception as e:
        # 有异常,回滚事务
        conn.rollback()
    cursor.close()
    conn.close()

    7,查

    单条数据


    # 得到一个可以执行SQL语句的光标对象 cursor = conn.cursor() # 查询数据的SQL语句 sql = "SELECT id,name,age from USER1 WHERE id=1;" # 执行SQL语句 cursor.execute(sql) # 获取单条查询数据 ret = cursor.fetchone() cursor.close() conn.close() # 打印下查询结果 print(ret)
    多条数据


    # 得到一个可以执行SQL语句的光标对象 cursor = conn.cursor() # 查询数据的SQL语句 sql = "SELECT id,name,age from USER1;" # 执行SQL语句 cursor.execute(sql) # 获取多条查询数据 ret = cursor.fetchall() cursor.close() conn.close() # 打印下查询结果 print(ret)

    三,控制光标

    # 可以获取指定数量的数据
    cursor.fetchmany(3)
    # 光标按绝对位置移动1
    cursor.scroll(1, mode="absolute")
    # 光标按照相对位置(当前位置)移动1
    cursor.scroll(1, mode="relative")
  • 相关阅读:
    mysql关联更新like,CONCAT,Length 函数使用
    泛型类
    libwebp 解码 webp格式的图片或者动画【源码】
    windwos自带的 一个xml库 MSXML 兼容宽字符
    使用华为云的arm搭建gogs
    centos7 arm mysql 安装
    关于脱壳的一些笔记
    关于OD调试的一些笔记
    关于对《汇编语言》第3版 作者:王爽的阅读总结
    使用Proxifier + Fiddler 抓任何包
  • 原文地址:https://www.cnblogs.com/xuguangzong/p/8617403.html
Copyright © 2011-2022 走看看