一、批量插入
insert_data = [
[1, '张三', 84],
[2, '李四', 92],
[3, '王五', 75]
]
conn = pymysql.connect(host=host, user=user, password=pwd, database=database, charset='utf8')
cursor = conn.cursor()
sql = 'INSERT INTO student(stu_id, stu_name, stu_score) VALUES (%s, %s, %s)'
try:
res = cursor.executemany(sql, insert_data)
conn.commit()
except Exception as e:
print(e)
conn.rollback()
二、批量更新
update_data = [
[81, 1],
[90, 2],
[72, 3]
]
conn = pymysql.connect(host=host, user=user, password=pwd, database=database, charset='utf8')
cursor = conn.cursor()
sql = 'UPDATE student SET stu_score = (%s) WHERE student_id = (%s)'
try:
res = cursor.executemany(sql, update_data)
conn.commit()
except Exception as e:
print(e)
conn.rollback()