1. 程式人生 > 資料庫 >python 批量update mysql資料

python 批量update mysql資料

更新資料庫

import pymysql


db = pymysql.connect(host='localhost', user='root', password='123456', port=3306, db='user_info')
cursor = db.cursor()
user_dict = {}
timeList = []
sql = "select create_time, user_id from user; "
cursor.execute(sql)
timeinfo = cursor.fetchall()
for item in timeinfo:
    user_dict[item[1]] = item[0]

for item in user_dict.keys():
    createtime = user_dict[item]
    userId = item
    updatesql = "update user_setting set create_time = '%s' where user_id = %s" % (createtime, userId)
    cursor.execute(updatesql)
    db.commit()