-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) 
2
  • Can you post the query that you have used to create the table? Commented Jun 13, 2017 at 3: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 Commented Jun 13, 2017 at 8:57

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

I got an errror when trying to do your way: TypeError: 'use_unicode' is an invalid keyword argument for this function
Works for me with MySQL-python==1.2.5. Which version do you use?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.