I need to insert into a MySQL 5.7 database strings with several non-ASCII characters, like è or … or even other alphabets, using a Python3 script. I converted all the columns of the interested table in utf8mb4.
Connecting with
db1 = MySQLdb.connect (
host="host1",
user="user1",
passwd="secret",
db="db1"
)
cursor1 = db1.cursor()
cursor1.execute("USE db1")
I can correctly store strings with è. Strings with … generate instead the following error:
UnicodeEncodeError: 'latin-1' codec can't encode character '\u2026' in position 1022: ordinal not in range(256)
Vice-versa, connecting with
db1 = MySQLdb.connect (
host="host1",
user="user1",
passwd="secret",
db="db1"
)
cursor1 = db1.cursor()
cursor1.execute("USE db1")
cur.execute("SET NAMES utf8mb4;")
cur.execute("SET CHARACTER SET utf8mb4;")
cur.execute("SET character_set_connection=utf8mb4;")
an error due to è is generated:
_mysql_exceptions.OperationalError: (1366, "Incorrect string value: '\\xE8 string...' for column 'column1' at row 1")
è has hex code E8.
What's wrong?