CREATE TABLE `t1` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`first_name` varchar(20) NOT NULL,
`last_name` varchar(20) NOT NULL,
`age` int(11) NOT NULL,
`male` tinyint(4) NOT NULL,
`other` int(11) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `udx_fl_name` (`first_name`,`last_name`),
UNIQUE KEY `udx_other` (`other`),
KEY `idx_age` (`age`)
) ENGINE=InnoDB AUTO_INCREMENT=25 DEFAULT CHARSET=utf8mb4
import mysql.connector
cnx = mysql.connector.connect(user='xxx', password='xxxx',
host='10.10.10.1',
database='test')
cursor = cnx.cursor()
sql_replace = '''
REPLACE INTO t1
(`id`, `first_name`, `last_name`, `age`, `male`, `other`)
VALUES
(%(id)s, %(first_name)s, %(last_name)s, %(age)s, %(male)s, %(other)s)
'''
values = []
for i in range(20, 25):
value = {}
value['id'] = i
value['first_name'] = i
value['last_name'] = i
value['age'] = i
value['male'] = i
value['other'] = i
values.append(value)
cursor.executemany(sql_replace, values)
cnx.commit()
cursor.close()
cnx.close()
BEGIN
/*!*/;
# at 3104
#180531 11:23:47 server id 3306137012 end_log_pos 3233 CRC32 0xf6760f60 Rows_query
# REPLACE INTO t1
# (`id`, `first_name`, `last_name`, `age`, `male`, `other`)
# VALUES
# (20, 20, 20, 20, 20, 20)
......
#180531 11:23:47 server id 3306137012 end_log_pos 3475 CRC32 0x7b1a12ea Rows_query
# REPLACE INTO t1
# (`id`, `first_name`, `last_name`, `age`, `male`, `other`)
# VALUES
# (21, 21, 21, 21, 21, 21)
......
#180531 11:23:47 server id 3306137012 end_log_pos 3717 CRC32 0xffc8053e Rows_query
# REPLACE INTO t1
# (`id`, `first_name`, `last_name`, `age`, `male`, `other`)
# VALUES
# (22, 22, 22, 22, 22, 22)
......
#180531 11:23:47 server id 3306137012 end_log_pos 3959 CRC32 0xf1ec47e8 Rows_query
# REPLACE INTO t1
# (`id`, `first_name`, `last_name`, `age`, `male`, `other`)
# VALUES
# (23, 23, 23, 23, 23, 23)
......
#180531 11:23:47 server id 3306137012 end_log_pos 4201 CRC32 0x1d24c291 Rows_query
# REPLACE INTO t1
# (`id`, `first_name`, `last_name`, `age`, `male`, `other`)
# VALUES
# (24, 24, 24, 24, 24, 24)
......
#180531 11:23:51 server id 3306137012 end_log_pos 4345 CRC32 0x820b85e3 Xid = 30
COMMIT/*!*/;
......
RE_SQL_INSERT_STMT = re.compile(
r"({0}|\s)*INSERT({0}|\s)*INTO\s+[`'\"]?.+[`'\"]?(?:\.[`'\"]?.+[`'\"]?)"
r"{{0,2}}\s+VALUES\s*\(.+(?:\s*,.+)*\)".format(SQL_COMMENT),
re.I | re.M | re.S)
......
......
if re.match(RE_SQL_INSERT_STMT, operation):
if not seq_params:
self._rowcount = 0
return
stmt = self._batch_insert(operation, seq_params)
if stmt is not None:
return self.execute(stmt)
rowcnt = 0
......
RE_SQL_REPLACE_STMT = re.compile(
r"({0}|\s)*REPLACE({0}|\s)*INTO\s+[`'\"]?.+[`'\"]?(?:\.[`'\"]?.+[`'\"]?)"
r"{{0,2}}\s+VALUES\s*\(.+(?:\s*,.+)*\)".format(SQL_COMMENT),
re.I | re.M | re.S)
RE_SQL_INSERT_IGNORE_STMT = re.compile(
r"({0}|\s)*INSERT({0}|\s)*IGNORE({0}|\s)*INTO\s+[`'\"]?.+[`'\"]?(?:\.[`'\"]?.+[`'\"]?)"
r"{{0,2}}\s+VALUES\s*\(.+(?:\s*,.+)*\)".format(SQL_COMMENT),
re.I | re.M | re.S)
if (re.match(RE_SQL_INSERT_STMT, operation) or
re.match(RE_SQL_REPLACE_STMT, operation) or
re.match(RE_SQL_INSERT_IGNORE_STMT, operation)):
if not seq_params:
self._rowcount = 0
return
stmt = self._batch_insert(operation, seq_params)
if stmt is not None:
return self.execute(stmt)
rowcnt = 0