PYODBC 损坏 utf8 数据(从 MYSQL information_schema DB 读取)
编辑: 我完全重新设计了这个问题,以反映我对问题的更好理解
PYODBC+MYSQL 命令用于获取数据库中的所有表名
cursor.execute("select table_name from information_schema.tables where
table_schema='mydbname'")
结果是一个 unicode 字符串列表,每个字符串中每隔一个字符被省略细绳。
information_schema
数据库是 utf8,尽管我的表名是纯 ascii。从我的数据库中读取 latin1 效果很好。执行 set character_set_* = 'utf8'
没有帮助。
从 C++/ODBC 测试程序执行相同的查询效果很好。
您知道 pyodbc 如何处理字符编码吗?使用 utf8 数据库时采用什么编码?
我在 Linux 上使用 UnixODBC、python 2.6.4、pyodbc 2.1.7
EDIT:
I completely reworked this question to reflect my better understanding of the problem
The PYODBC+MYSQL command used to fetch all table names in my DB
cursor.execute("select table_name from information_schema.tables where
table_schema='mydbname'")
The result is a list of unicode strings with every second character omitted, in each string.
The information_schema
DB is utf8, although my table names are pure ascii. Reading from my DB which is latin1 works fine. Executing set character_set_* = 'utf8'
does not help.
Executing the same query from a C++/ODBC test program works fine.
Do you know how pyodbc
works wrt to character encoding? What encoding does it assume when working with a utf8 DB?
I work on Linux with UnixODBC, python 2.6.4, pyodbc 2.1.7
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
ODBC 规范仅允许两种编码:ASCII 和 UCS-2。 ODBC 驱动程序的工作是将数据库所在的任何内容转换为这两者之一,但我发现大多数 ODBC 驱动程序作者不明白它应该如何工作。
执行查询时,pyodbc 不要求任何编码。它执行查询,然后向驱动程序询问每列的数据类型。如果数据类型是 Unicode,它将读取缓冲区并将其视为 UCS2。如果数据类型是 ASCII,它将读取缓冲区并将其视为 ASCII。
存储格式应该是无关的。
The ODBC specification only allows two encodings: ASCII and UCS-2. It is the job of the ODBC driver to convert whatever the database is in to one of those two, but I find most ODBC driver authors don't understand how it is supposed to work.
When a query is executed, pyodbc does not ask for any encoding. It executes the query and then asks the driver for the data type of each column. If the data type is Unicode, it will read the buffer and treat it as UCS2. If the data type is ASCII, it will read the buffer and treat it as ASCII.
The storage format is supposed to be irrelevant.