如何使用 pyodbc 对 Unicode 字符串进行编码以保存到 SAS 数据集?

发布于 2024-09-02 14:01:54 字数 351 浏览 0 评论 0原文

我使用 Python 来读取和写入 SAS 数据集,并使用 pyodbc 和 SAS ODBC 驱动程序。我可以很好地加载数据,但是当我保存数据时,使用以下内容:

cursor.execute('insert into dataset.test VALUES (?)', u'testing')

... 我收到 pyodbc.Error: ('HY004', '[HY004] [Microsoft][ODBC Driver Manager] SQL数据类型超出范围 (0) (SQLBindParameter)') 错误。

问题似乎是我传递了一个 unicode 字符串;我需要做什么来处理这个问题?

I'm using Python to read and write SAS datasets, using pyodbc and the SAS ODBC drivers. I can load the data perfectly well, but when I save the data, using something like:

cursor.execute('insert into dataset.test VALUES (?)', u'testing')

... I get a pyodbc.Error: ('HY004', '[HY004] [Microsoft][ODBC Driver Manager] SQL data type out of range (0) (SQLBindParameter)') error.

The problem seems to be the fact I'm passing a unicode string; what do I need to do to handle this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

海螺姑娘 2024-09-09 14:01:54

您知道您的数据库需要什么字符编码吗?如果是这样,您可以在执行查询之前尝试对 Unicode 字符串进行编码。因此,如果您的数据库需要 utf-8 字符串,您可以尝试以下操作:

encoding = 'utf-8' # or latin1 or cp1252 or something
s = u'testing'.encode(encoding)
cursor.execute('insert into dataset.test VALUES (?)', s)

Do you know what character encoding your database is expecting? If so, you could try encoding your Unicode string before executing the query. So if your database is expecting utf-8 strings, you could try something like:

encoding = 'utf-8' # or latin1 or cp1252 or something
s = u'testing'.encode(encoding)
cursor.execute('insert into dataset.test VALUES (?)', s)
爱,才寂寞 2024-09-09 14:01:54

您说“我可以很好地加载数据”...这是否意味着您可以加载包含不在您的平台上使用的本机编码中的字符的数据(大概是 Windows 上的 cp1252,但请确认)? SAS 数据集第一列的 SAS 数据类型是什么?

SAS 文档中的这篇文章旨在展示如何找出 SAS 数据集中使用的编码。

SAS ODBC 文档。但是,您似乎没有使用 SAS ODBC(即访问非 SAS 数据的 SAS 语言脚本)。

You say "I can load the data perfectly well" ... does this mean that you can load data that contains characters that are NOT in the native encoding used on your platform (presumably cp1252 on Windows, but please confirm)? What is the SAS datatype of the first column of your SAS dataset?

This article in the SAS docs purports to show how you can find out the encoding used in a SAS dataset.

Encoding is mentioned in the SAS ODBC documentation. However you don't appear to be using SAS ODBC (i.e. SAS-language script accessing non-SAS data).

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