Python & MySql: Unicode and Encoding
I think that your MYSQLdb python library doesn't know it's supposed to encode to utf8, and is encoding to the default python system-defined charset latin1
.
When you connect()
to your database, pass the charset='utf8'
parameter. This should also make a manual SET NAMES
or SET character_set_client
unnecessary.
First, make sure you are assigning the charset
and use_unicode
parameters when making your MySQL connection:
conn = mysql.connect(host='127.0.0.1', user='user', passwd='passwd', db='db', charset='utf8', use_unicode=True)
Secondly, use prepared statements when actually querying the database. Below is an example INSERT query of a string containing a unicode character.
cursor.execute('INSERT INTO mytable VALUES (null, %s)', ('Some string that contains unicode: ' + unichr(300),))