Python RPA 数据库篇 7 - MySQL 篇 3 - 增删改查 2

Python RPA 数据库篇 7 - MySQL 篇 3 - 增删改查 2

pymysql 给更新据

代码

#!/usr/bin/env Python3
# -*- coding: utf-8 -*-
# @Software: PyCharm
# @virtualenv:workon
# @contact: contact information
# @Desc:Code descripton
__author__ = '未昔/AngelFate'
__date__ = '2019/8/29 20:35'

import pymysql

# 3.更新操作
db = pymysql.connect(host="localhost", user="root",
                     password="", db="novel", port=3306)

# 使用cursor()方法获取操作游标
cur = db.cursor()

sql_update = "update Test set t_name = '%s' where id = %d"

try:
    cur.execute(sql_update % ("更新小明明", 4))  # 像sql语句传递参数
    # 提交
    db.commit()
except Exception as e:
    # 错误回滚
    db.rollback()
finally:
    db.close()

结果

Python RPA 数据库篇 7 - MySQL 篇 3 - 增删改查 2