蒹葭苍苍,白露为霜。
所谓伊人,在水一方。

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()

 

赞(1) 打赏
未经允许不得转载:酷居科技 » python中使用excutemany执行update语句,批量更新

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

锦瑟无端五十弦,一弦一柱思华年

酷居科技联系我们

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏