python中使用excutemany执行update语句,批量更新


python中使用excutemany执行update语句,批量更新



# coding:utf8
import pymysql
import logging connection = pymysql.connect(host=HOST, port=3306, user=USER, password=PASSWORD,
db=DATABASE,charset='utf8mb4',cursorclass=pymysql.cursors.DictCursor)
cursor = connection.cursor()
flag_List = ["A", "B", "C", "D"] # 存储flage的值
id_list = ["1", "2", "3", "4"] # 存储id的值
commit_id_list = [(flag_List[i], id_list[i]) for i in range(len(id_list))]
# commit_id_list必须是list[tuple(),tuple()...]或tuple(tuple(),tuple()...)的形式 with connection.cursor() as cursor:
try:
sql = "update mydatabase SET flag=(%s) where id=(%s)"
cursor.executemany(sql, commit_id_list) # commit_id_list上面已经说明
connection.commit()
except:
logging.exception("exception")
connection.rollback()
connection.close()
上一篇:C# 绘制统计图(柱状图, 折线图, 扇形图)【转载】


下一篇:C# 绘制统计图(柱状图, 折线图, 扇形图)