Solution of "UnicodeEncodeError:'latin-1' codec can't encode character ..." while working with MySQLdb and Python.

Posted on Wed 28 March 2012 in General

I got the error of "UnicodeEncodeError:'latin-1' codec can't encode character ..." while i was inserting data into mysql with MySQLdb/Python.

Below is the solution:

import MySQLdb as litefire
conx = litefire.connect('127.0.0.1', 'root', '' , 'litefire2' );
conx.set_character_set('utf8')
conr=conx.cursor()
conr.execute('SET NAMES utf8;')
conr.execute('SET CHARACTER SET utf8;')
conr.execute('SET character_set_connection=utf8;')

Enjoy,