0

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?

1 Answer 1

1

You should try setting the encoding to UTF8 while connecting to the DB by doing something like this:

db1 = MySQLdb.connect (
host="host1",
user="user1",
passwd="secret",
db="db1"
use_unicode=True, 
charset="utf8"
)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.