我们在使用pymysql执行mysql语句的时候,可以使用批量插入的功能,例如我要插入如下三条内容到表test中:
name: kingname, salary:99999, phone_number...name: 王小二, salary:0, phone_number:7865432
name: 张小三, salary:44444, phone_number:88997766
那么我用Python插入数据...7865432), ('张小三', 44444, 88997766)]
cursor.executemany(sql, params)
connection.commit()
正常情况下这样插入是没有问题的...但是,如果字段 phone_number是一个 unique字段,这样插入就会导致报错。现在的需求是,如果phone_number重复,那么就直接覆盖 name和 salary字段。...于是,你可能会想着把MySQL写成这样:
with connection.cursor() as cursor:
sql = 'insert into test (`name`, `salary