【发布时间】:2015-02-18 09:23:26
【问题描述】:
我正在尝试将来自 mysql.com 的 MySQL Connector/Python 与 Python 3 一起使用。
我有 UTF-8 编码的表,当我获取行时,我的所有字符列都返回了 bytearray。这会造成一些混乱。
如何直接获取str?
更新:
# -*- coding: utf-8 -*-
import mysql.connector
con = mysql.connector.connect( user ="root", db = "vg_site_db", charset = 'utf8' )
cursor = con.cursor()
sql = """select caption from domains
"""
cursor.execute( sql )
row = cursor.fetchone()
while row is not None:
print( row )
row = cursor.fetchone()
输出:
(bytearray(b'ezsp.ru'),)
(bytearray(b'eazyshop.ru'),)
(bytearray(b'127.0.0.1:8080'),)
(bytearray(b'rmsvet.ru'),)
我想要:
('ezsp.ru',)
('eazyshop.ru',)
('127.0.0.1:8080',)
('rmsvet.ru',)
UPD2:
我的表使用COLLATE utf8_bin。
【问题讨论】:
-
显示读取数据库内容的python代码
-
我更新帖子并添加代码示例
-
我还在 Python 2.7 中从 cursor.fetchone() 获取字节数组。通过
str(row[0].decode())将它们传递给 Python 2.7 和 3.4 中的原生字符串。 MySQL made a change 在连接器版本 2 中。
标签: python mysql python-3.x utf-8 collation