I connect to MySQL and retrieve usernames containing 'Ö', 'ğ', 'Ş', etc. It works fine with MySQL or PHP but in Python 2.6.8, an error occur. Here is my code:
#C:\Python27\Lib\encodings
#-*- coding: utf-8 -*-
conn = MySQLdb.Connect(host="localhost", user="root", passwd="mypass", db="mydb", charset="utf8", init_command="SET NAMES UTF8")
cursor = conn.cursor(MySQLdb.cursors.DictCursor)
cursor.execute("select * from users");
tmpDict=cursor.fetchallDict()
print tmpDict[0]['NAME'].decode('utf8')
I expect 'Ömer Şirin' here but instead I get the following error:
'ascii' codec can't encode character u'\xd6' in position 0: ordinal not in range(128)
How can I fix this?