Why am I getting this UnicodeEncodeError when I am inserting into the MySQL database?

UnicodeEncodeError: 'ascii' codec can't encode character u'\xe9' in position 2: ordinal not in range(128)

I changed my database default to be utf-8, and not "latin"....but this error still occurs. why?

This is in my.cnf. Am I doing this wrong? I just want EVERYTHING TO BE UTF-8.

init_connect='SET collation_connection = utf8_general_ci'
init_connect='SET NAMES utf8'
character-set-server = utf8
collation-server = utf8_general_ci


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()
        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')
        return value

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.

Need Your Help

Why does Oracle ODP.Net automattically commit inserted records when using parameter array binding?

arrays oracle binding parameters odp.net

Strange issue. I have a managed OPD.Net application that calls a stored procedure to insert records. When I call the procedure normally and rollback the transaction, the records are not saved to the

Django Crispy Forms Add Div Around Submit Button

python django django-crispy-forms

Using Django Crispy Forms I would like to add a class around my submit button like this: