Python、Django 和 pyodbc:无效字符

发布于 2024-10-25 21:25:39 字数 524 浏览 2 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

峩卟喜欢 2024-11-01 21:25:39

您几乎没有给出任何线索,但合理的猜测是:

您需要使用正确编码将 MS SQL Server 数据解码为 un​​icode,并(不一定立即)将其编码为“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 not cp125x? True latin1 on an MS product is highly unlikely. Your errant byte '\x93' when decoded as cp1252 (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 as latin1 produces U+0093 which is some arcane control character whose usage in practice is as rare as hens' teeth.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文