-1

I'm new to python, I have a trouble reading data from mysql. Here in mysql, my table stores destination:

    |id  | Destination|
    |1   |Hà Nội      |
    |2   |Hồ Chí Minh |

... but when I read it from python its return this: ' u\'H\xe0 N\u1ed9i\'' for Hà Nội. Is there any way to convert it back to original string: 'Hà Nội' here is my code to open connection and select data:

def __init__(self):
    self.mySQLConnection = mysql.connector.connect(user='root', password='', host='localhost',database='traveltrend',charset='utf8',use_unicode=True)
    self.cursor = self.mySQLConnection.cursor(buffered=True)

 def getDest(self):
    self.cursor.execute("SELECT ID, ThanhPho FROM diadiem ")
    row = self.cursor.fetchone()
    listDest={}
    while row is not None:
        request = str(row)
        request = request.strip('()')
        request = request.split(',')
        encoding = "utf-8"
        on_error = "replace"
        place= request[1].encode(encoding,on_error)
Sonx
  • 1
  • Can you post the query that you have used to create the table? – Arun Jun 13 '17 at 03:53
  • I used php my admin to create the table, but here is the sql code generated CREATE TABLE `destination` ( `ID` int(11) NOT NULL, `ThanhPho` varchar(50) CHARACTER SET utf8 COLLATE utf8_vietnamese_ci NOT NULL ) ENGINE=MyISAM DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_vietnamese_ci; @arun – Sonx Jun 13 '17 at 08:57

1 Answers1

0

I connect with MySQLdb package. I read from MySQL with unicode characters and this worked for me.

from MySQLdb import connect
connection = connect(user='root', passwd='', host='localhost',db='traveltrend',use_unicode=True)

Hope this helps.