MySQLdb not playing nice with unicode

Roy Smith roy at panix.com
Sat Mar 30 17:19:39 CET 2013


5.1.63-log MySQL Community Server (GPL)
Python 2.7.3
MySQL-python==1.2.3

I've got a table:

CREATE TABLE `songza_temp` (
  `s` varchar(1000) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

When I run this code:

--------------------------------------------
db = MySQLdb.connect(host=db_host,
                     db=db_db,
                     user=db_user,
                     passwd=db_passwd,
                     charset='utf8',
                     sql_mode='TRADITIONAL')
cursor = db.cursor()

baloon = u'\U0001f388'
print "baloon=%s, repr=%r, length=%d" % (baloon, baloon, len(baloon))
cursor.execute("insert into songza_temp (s) values (%s)", [baloon])
--------------------------------------------

I get:

baloon=<ÿàþ, repr=u'\U0001f388', length=1
Traceback (most recent call last):
  File "./try.py", line 21, in <module>
    cursor.execute("insert into songza_temp (s) values (%s)", [baloon])
  File "/usr/lib/python2.7/dist-packages/MySQLdb/cursors.py", line 174, 
in execute
    self.errorhandler(self, exc, value)
  File "/usr/lib/python2.7/dist-packages/MySQLdb/connections.py", line 
36, in defaulterrorhandler
    raise errorclass, errorvalue
_mysql_exceptions.OperationalError: (1366, "Incorrect string value: 
'\\xF0\\x9F\\x8E\\x88' for column 's' at row 1")

My unicode-fu is a bit weak.  Are we looking at a Python problem, a 
MySQLdb problem, or a problem with the underlying MySQL server?  We've 
certainly inserted utf-8 data before without any problems.  It's 
possible this is the first time we've tried to handle a character 
outside the BMP.



More information about the Python-list mailing list