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()
相关推荐
王艺强 2020-11-17
anchongnanzi 2020-09-21
84296033 2020-09-15
heimu 2020-08-02
herohope 2020-07-18
mrandy 2020-07-04
Jaystrong 2020-06-27
89921334 2020-06-26
debugjoker 2020-06-17
Linkaibin 2020-06-14
fanhuasijin 2020-06-14
Laxcus大数据技术 2020-06-13
hanshangzhi 2020-06-10
rainchxy 2020-06-07
Jerry 2020-06-01
lilygg 2020-05-29
lclcsmart 2020-05-27