python中使用excutemany执行update语句,批量更新
Posted 小学弟-
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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()
以上是关于python中使用excutemany执行update语句,批量更新的主要内容,如果未能解决你的问题,请参考以下文章