Quantcast
Viewing all articles
Browse latest Browse all 33

Python with MySQL special characters issues (tildes, etc) [SOLVED]

Some days ago a friend of mine who is learning python asked for my help. He was experimenting issues with spanish special characters (such as tildes -á,é,í,ó,ú- and -ñ-) when retrieving data from a MySQL database.

When I looked at the code, I saw he was connecting to MySQL using MySQLdb connect function as in:

import MySQLdb as mdb
con = mdb.connect(host='localhost',user='myuser',passwd='mypassword',db='mydatabase')

And he was astonished because I was able to solve his problems just by changing that to:

import MySQLdb as mdb
con = mdb.connect(host='localhost',user='myuser',passwd='mypassword',db='mydatabase', charset='utf8', use_unicode=False)

So don’t underestimate the power of official documentation and please, RTFM!


Viewing all articles
Browse latest Browse all 33

Trending Articles