I'm having issues with my Python when calling data from my MySQL database.
The database is set as UTF-8 and is containing, special letters such as 'Æ' and 'Ø'.
This is the code i use to call the Usernames from my Table
# -*- coding: UTF-8 -*-
import pymysql
db = pymysql.connect(
host="localhost",
user="root",
password="XXXXXXXX",
db="pythonconnectiontest",
charset="utf8"
)
cursor = db.cursor()
cursor.execute("SELECT Username FROM Account")
numrows = cursor.rowcount
for i in range (numrows):
row = cursor.fetchone()
print row
The expected output is:
ThisTextIsBrøken
Tæst
Word
Actual output:
(u'ThisTextIsBr\xf8ken',)
(u'T\xe6st',)
(u'Word',)
I do get that the 'U' in front of the value, indicates that this is UNICODE and i probably just need to make python interpret the text correctly with encode() or decode().
I've spent many hours trying to figure this out, and the solution is probably very simple.
I hope someone can help me fix this.
Thanks for reading.
print unicode(row)it might solve your problemSELECT HEX(col), col FROM ...If 'Tæst' comes out54657374, then you really have latin1 encoding in the table! If everything is utf8, then the hex will be54C3A67374.