I want to store an address (type string
) into a database field (type VARCHAR
).
The problem is that sometimes the address has Non-ASCII characters, so I did the next to take them:
in my .py file
import sys
reload(sys)
sys.setdefaultencoding('utf8')
# Other imports...
# The connection with the DB
conn = MySQLdb.connect(host="127.0.0.1", user="myUser", passwd="myPass", db="myDB", charset='utf8', init_command='SET NAMES UTF8')
# More code here...
address2 = address.encode('utf-8')
print "\nTHE ADDRESS TYPE IS -> ", type(address), "\n"
print "\nTHE ADDRESS IS -> ", address, "\n"
print "\nTHE ADDRESS2 IS -> ", address2, "\n"
self.conn.cursor().execute("""INSERT INTO myTable (BLA, BLA, ADDRESS) VALUES (%s, %s, %s)""", (bla, bla, address2))
# The rest of the code...
So when I run the code, I get the following after the prints
:
THE ADDRESS TYPE IS -> <type 'str'>
THE ADDRESS IS -> Francesc Aragó, 2, 07872 Es Caló, España
THE ADDRESS2 IS -> Francesc Aragó, 2, 07872 Es Caló, España
but when I go to my table in the db, and access that field, I get this
Francesc Arag?, 2, 07872 Es Cal?, Espa?a
I've tried changing the address2
for address
in the mysql query, along with many other tries, but I've got nothing so far...