4
我无法从mysql查询中获取unicode值。Python,mysqldb和unicode
这里是我如何做到这一点现在:
>>> from MySQLdb import connect
>>> from MySQLdb.cursors import DictCursor
>>> con = connect(
passwd = "****",
db = 'my_db',
user = "db_user",
host = "localhost",
cursorclass = DictCursor,
use_unicode=True,
charset="utf8"
)
>>> cursor = con.cursor()
>>> cursor.execute (u'Select * from basic_applet')
>>> res = cursor.fetchall()
>>> print(res)
({'Title_de': 'test title', .... })
>>> type(res[0]['Title_de'])
<type 'str'>
正如你所看到的,它没有返回的Unicode。
在我的表结构中,Title_de被设置为unicode。
IDbasic_applet int(10)...
Title_de varchar(75) utf8_bin
我真的不知道我在做什么错,任何帮助都会非常受欢迎。
由于提前,
西蒙
据我所知,事实是,我认为有可能要求unicode字符串,而不是字节字符串。你知道use_unicode = True的效果吗? –
它对我来说有理想的效果。我的列定义包含'CHARACTER SET UTF8'而不是'utf8_bin'。 Afaik这是唯一的区别。 – pvoosten
@SimonRolin问他的问题已经有一段时间了,但仅仅是为了记录MySQLdb.connect(use_unicode = True,charset ='utf8',...)'使得MySQL返回unicode。 – mcrisc