码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2019-12-02 10:30:40      阅读:518      评论:0      收藏:0      [点我收藏+]

标签:pytho   flag   cep   data   password   art   ORC   ack   _id   


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语句,批量更新

标签:pytho   flag   cep   data   password   art   ORC   ack   _id   

原文地址:https://www.cnblogs.com/xiao-xue-di/p/11969138.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!