Python、Django 和 pyodbc:无效字符
我正在使用 pyodbc 连接到 MS SQL Server 数据库。我得到的错误如下
invalid byte sequence for encoding "UTF8": 0x93
HINT: This error can also happen if the byte sequence does not match the
encoding expected by the server, which is controlled by "client_encoding".
SQL 数据库是使用 Latin1 编码的,我将 postgres 与 django 一起使用,它需要 UTF8。
我对使用 pyodbc 很陌生,无法解决这个问题。我试图过滤大量的谷歌搜索,但没有成功。一些帮助将不胜感激。
编辑
Postgres 数据库是该项目的主数据库。我希望能够从 SQL Server 提取数据。不过,此过程不会经常执行...
发生错误的点是从 SQL Server 数据库读取数据时发生的
I am connecting to a MS SQL server database using pyodbc. The error im getting is the following
invalid byte sequence for encoding "UTF8": 0x93
HINT: This error can also happen if the byte sequence does not match the
encoding expected by the server, which is controlled by "client_encoding".
The SQL database is encoded using Latin1 and I am using postgres with django, which expects UTF8.
I am very new to using pyodbc and cannot solve this problem. i have attempted to filter through piles of google searches but with no luck. Some help would be greatly appreciated.
EDIT
The Postgres db is the main db for the project. I want to be able to pull data from the SQL Server. This process will not be done often though...
The point at which the error occurs is from the read from the SQL Server db
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您几乎没有给出任何线索,但合理的猜测是:
您需要使用正确编码将 MS SQL Server 数据解码为 unicode,并(不一定立即)将其编码为“UTF-8”用于传输到 postgres。
是什么让您认为 SQL Server 数据库上使用的编码是
latin1
而不是cp125x
? MS 产品上出现真正的latin1
的可能性很小。当您的错误字节'\x93'
解码为cp1252
(通常的嫌疑人)时,会给出U+201C左双引号,通常在例如MS Word中使用,并且通常会粘贴到最终进入数据库的数据。解码为latin1
会产生 U+0093,这是一些神秘的控制字符,其在实践中的使用就像母鸡的牙齿一样罕见。You have given next to no clues but a reasonable guess is:
You need to decode your MS SQL Server data to unicode using the correct encoding, and (not necessarily immediately) encode it as 'UTF-8' for transmission to postgres.
What makes you think that the encoding used on the SQL Server db is
latin1
and notcp125x
? Truelatin1
on an MS product is highly unlikely. Your errant byte'\x93'
when decoded ascp1252
(the usual suspect) gives U+201C LEFT DOUBLE QUOTATION MARK, commonly used in e.g. MS Word and commonly found pasted into data which ends up in a db. Decoding aslatin1
produces U+0093 which is some arcane control character whose usage in practice is as rare as hens' teeth.