Skip to content

Commit ed4bfcc

Browse files
committed
BUG24659561: Fix MySQLCursor.executemany() when using utf8mb4 charset
Inserting multiple rows into a table will fail with a LookupError when using a connection with utf8mb4 charset. This patch changes the charset used in MySQLCursor.executemany() to the Python charset for current connection. A test case was added for regression.
1 parent 656a759 commit ed4bfcc

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

lib/mysql/connector/cursor.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -589,11 +589,11 @@ def remove_comments(match):
589589
"Failed rewriting statement for multi-row INSERT. "
590590
"Check SQL syntax."
591591
)
592-
fmt = matches.group(1).encode(self._connection.charset)
592+
fmt = matches.group(1).encode(self._connection.python_charset)
593593
values = []
594594

595595
try:
596-
stmt = operation.encode(self._connection.charset)
596+
stmt = operation.encode(self._connection.python_charset)
597597
for params in seq_params:
598598
tmp = fmt
599599
if isinstance(params, dict):

tests/test_bugs.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4460,3 +4460,27 @@ def test_cmd_query_iter(self):
44604460
results.append(result)
44614461
if "columns" in result:
44624462
results.append(self.cnx.get_rows())
4463+
4464+
4465+
class BugOra24659561(tests.MySQLConnectorTests):
4466+
"""BUG#24659561: LOOKUPERROR: UNKNOWN ENCODING: UTF8MB4
4467+
"""
4468+
def setUp(self):
4469+
config = tests.get_mysql_config()
4470+
config["charset"] = "utf8mb4"
4471+
self.tbl = "BugOra24659561"
4472+
self.cnx = connection.MySQLConnection(**config)
4473+
self.cur = self.cnx.cursor()
4474+
self.cur.execute("DROP TABLE IF EXISTS {0}".format(self.tbl))
4475+
self.cur.execute("CREATE TABLE {0} (id INT, name VARCHAR(100))"
4476+
"".format(self.tbl))
4477+
4478+
def tearDown(self):
4479+
self.cur.execute("DROP TABLE IF EXISTS {0}".format(self.tbl))
4480+
self.cur.close()
4481+
4482+
def test_executemany_utf8mb4(self):
4483+
self.cur.executemany(
4484+
"INSERT INTO {0} VALUES (%s, %s)".format(self.tbl),
4485+
[(1, "Nuno"), (2, "Amitabh"), (3, "Rafael")]
4486+
)

0 commit comments

Comments
 (0)