zoukankan      html  css  js  c++  java
  • pymysql

    pymysql

    安装

    pip install pymysql

    表操作

    创建表

    # 导入pymysql模块
    import pymysql
    # 连接database
    conn = pymysql.connect(host="127.0.0.1", user="root",password="root",database="test",charset="utf8")
    # 得到一个可以执行SQL语句的光标对象
    cursor = conn.cursor()
    # 定义要执行的SQL语句
    sql = """
    CREATE TABLE USER1 (
    id INT auto_increment PRIMARY KEY ,
    name varchar (10) NOT NULL UNIQUE,
    age TINYINT NOT NULL
    )ENGINE=innodb DEFAULT CHARSET=utf8;
    """
    # 执行SQL语句
    cursor.execute(sql)
    # 关闭光标对象
    cursor.close()
    # 关闭数据库连接
    conn.close()
    View Code

    数据操作

    增加

    单条

    # 导入pymysql模块
    import pymysql
    # 连接database
    conn = pymysql.connect(host="127.0.0.1", user="root",password="root",database="test",charset="utf8")
    # 得到一个可以执行SQL语句的光标对象
    cursor = conn.cursor()
    sql = "INSERT INTO USER1(name, age) VALUES (%s, %s);"
    username = "pig"
    age = 16
    # 执行SQL语句
    ret=cursor.execute(sql, [username, age])
    print(ret)
    # 提交事务
    conn.commit()
    cursor.close()
    conn.close()
    View Code

    批量

    # 导入pymysql模块
    import pymysql
    # 连接database
    conn = pymysql.connect(host="127.0.0.1", user="root",password="root",database="test",charset="utf8")
    # 得到一个可以执行SQL语句的光标对象
    cursor = conn.cursor()
    sql = "INSERT INTO USER1(name, age) VALUES (%s, %s);"
    data = [("elephant", 18), ("dog", 20), ("cat", 21)]
    try:
        # 批量执行多条插入SQL语句
        cursor.executemany(sql, data)
        # 提交事务
        conn.commit()
    except Exception as e:
        # 有异常,回滚事务
        conn.rollback()
    cursor.close()
    conn.close()
    View Code

    插入时返回数据id

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

    删除

    # 导入pymysql模块
    import pymysql
    # 连接database
    conn = pymysql.connect(host="127.0.0.1", user="root",password="root",database="test",charset="utf8")
    # 得到一个可以执行SQL语句的光标对象
    cursor = conn.cursor()
    sql = "DELETE FROM USER1 WHERE id=%s;"
    try:
        cursor.execute(sql, [3])
        # 提交事务
        conn.commit()
    except Exception as e:
        # 有异常,回滚事务
        conn.rollback()
    cursor.close()
    conn.close()
    View Code

    修改

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

    查询

    # 导入pymysql模块
    import pymysql
    # 连接database
    conn = pymysql.connect(host="127.0.0.1", user="root",password="root",database="test",charset="utf8")
    # 得到一个可以执行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)
    View Code

    查询结果改为字典列表

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

    其他

    # 可以获取指定数量的数据
    cursor.fetchmany(3)
    # 光标按绝对位置移动1
    cursor.scroll(1, mode="absolute")
    # 光标按照相对位置(当前位置)移动1
    cursor.scroll(1, mode="relative")
    View Code
  • 相关阅读:
    .Net Frameworks versions
    noteJavascript::string2Date for new Date(str)
    noteMS questions
    note删除Visual Studio recent Projects list
    noteshow hidden files
    sql trick
    拯救MySql 签名
    数据结构——栈和队列
    the summary of loop structure in C
    the summury of array in C.
  • 原文地址:https://www.cnblogs.com/huay/p/11611044.html
Copyright © 2011-2022 走看看