[Answered ]-Why am I getting this UnicodeEncodeError when I am inserting into the MySQL database?

0👍

If you get an exception from Python then it’s nothing to do with MySQL — the error happens before the expression is sent to MySQL. I would presume that the MySQLdb driver doesn’t handle unicode.

If you are dealing with the raw MySQLdb interface this will be somewhat annoying (database wrappers like SQLAlchemy will handle this stuff for you), but you might want to create a function like this:

def exec_sql(conn_or_cursor, sql, *args, **kw):
    if hasattr(conn_or_cursor):
        cursor = conn_or_cursor.cursor()
    else:
        cursor = conn_or_cursor
    cursor.execute(_convert_utf8(sql), *(_convert_utf8(a) for a in args),
                   **dict((n, _convert_utf8(v)) for n, v in kw.iteritems()))
    return cursor

def _convert_utf8(value):
    if isinstance(value, unicode):
        return value.encode('utf8')
    else:
        return value

2👍

MySQLdb.connect(read_default_*) options won’t set the character set from default-character-set. You will need to set this explicitly:

MySQLdb.connect(..., charset='utf8')

Or the equivalent setting in your django databases settings.

👤Andrew

Leave a comment