Python知识分享网 - 专业的Python学习网站 学Python,上Python222
pymysql执行delete删除操作
发布于:2023-09-19 10:46:25

1小时学会 Python操作Mysql数据库之pymysql模块技术https://www.bilibili.com/video/BV1Dz4y1j7Jr

 

 

执行delete操作,雷同前面的update操作

 

from pymysql import Connection

con = None

try:
    # 创建数据库连接
    con = Connection(
        host="localhost",  # 主机名
        port=3306,  # 端口
        user="root",  # 账户
        password="123456",  # 密码
        database="db_python",  # 指定操作的数据库
        autocommit=True  # 设置自动提交
    )
    # 获取游标对象
    cursor = con.cursor()
    # 使用游标对象,执行sql语句
    cursor.execute("delete from t_student  WHERE id=5")
    # 确认提交
    # con.commit()
except Exception as e:
    print("异常:", e)
finally:
    if con:
        # 关闭连接
        con.close()

 

 

 

转载自: