Update: The real problem is that MySQL utf8 does not support four-byte UTF-8 characters.
There are several questions on this topic, but none of them seems to be my question exactly, except for maybe this one, where the accepted answer does not work for me.
I am coding in Python with the MySQLdb
module, and I want to put some text into a MySQL database. The database is configured for UTF-8, but the text occasionally contains non-UTF-8 four-byte-UTF-8 characters.
The Python code for the database modification looks like this:
connection = MySQLdb.connect(
'localhost',
'root',
'',
'mydatabase',
charset='utf8',
use_unicode=True)
cursor = connection.cursor()
cursor.execute(
'update mytable set entryContent=%s where entryName=%s',
(entryContent, entryName))
connection.commit()
And it currently produces this warning:
./myapp.py:233: Warning: Invalid utf8 character string: 'F09286'
(entry, word))
./myapp.py:233: Warning: Incorrect string value: '\xF0\x92\x86\xB7\xF0\x92...' for column 'entry' at row 1
(entryname, entrycontent))
When I look at what actually got into the database with the mysql
command-line client, I see the content truncated at the very first occurrence of a non-UTF-8 four-byte UTF-8 character.
I don't care about preserving the non-UTF-8 four-byte UTF-8 characters, so all I want to do is replace all non-UTF-8 four-byte UTF-8 characters with some other valid UTF-8 character, so I can put the text into the database.