Unicode String不能以Pymssql的形式传递给SQL查询

发布于 2025-01-26 16:10:17 字数 1141 浏览 3 评论 0原文

我很难通过参数将Unicode字符串传递到SQL查询。

启动连接并执行查询:

conn = pymssql.connect(server='serverName', port=1433, user='userName', password='password', database='databaseName')
cursor = conn.cursor()

hebrew_string = "הצפת מחסנית"
cursor.execute("SELECT userID FROM users WHERE userName IN %s", hebrew_string)

这将给我以下错误:

MSSQLDatabaseException: (102, b"Incorrect syntax near '\xd7\x94\xd7\xa6\xd7\xa4\xd7\xaa \xd7\x9e\xd7\x97\xd7\xa1\xd7\xa0\xd7\x99\xd7\xaa'.DB-Lib error message 20018, severity 15:\nGeneral SQL Server error: Check messages from the SQL Server\n")

我知道可以使用n'< hebrew_string>'轻松解决这一点,但是当我想制作动态查询时,这不是我想要的。

cursor.execute("SELECT userID FROM users WHERE userName IN (N'הצפת מחסנית')")

这些方法无法根据我的实验来起作用:

cursor.execute("SELECT userID FROM users WHERE userName IN N'%s'", hebrew_string)
cursor.execute("SELECT userID FROM users WHERE userName IN (N'%s')", hebrew_string)
cursor.execute("SELECT userID FROM users WHERE userName IN N%s", hebrew_string)

我正在使用Python3.8

任何答案都将不胜感激。谢谢!

I'm having trouble passing a Unicode string to an SQL query via parameter.

Initiate the connection and execute the query:

conn = pymssql.connect(server='serverName', port=1433, user='userName', password='password', database='databaseName')
cursor = conn.cursor()

hebrew_string = "הצפת מחסנית"
cursor.execute("SELECT userID FROM users WHERE userName IN %s", hebrew_string)

This will give me the below error:

MSSQLDatabaseException: (102, b"Incorrect syntax near '\xd7\x94\xd7\xa6\xd7\xa4\xd7\xaa \xd7\x9e\xd7\x97\xd7\xa1\xd7\xa0\xd7\x99\xd7\xaa'.DB-Lib error message 20018, severity 15:\nGeneral SQL Server error: Check messages from the SQL Server\n")

I know that this can be solved easily using N'<hebrew_string>', but as I want to make a dynamic query, this is not what I'm looking for.

cursor.execute("SELECT userID FROM users WHERE userName IN (N'הצפת מחסנית')")

And these methods did not work based on my experiment:

cursor.execute("SELECT userID FROM users WHERE userName IN N'%s'", hebrew_string)
cursor.execute("SELECT userID FROM users WHERE userName IN (N'%s')", hebrew_string)
cursor.execute("SELECT userID FROM users WHERE userName IN N%s", hebrew_string)

I'm using python3.8

Any answer would be appreciated. Thanks!

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

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

发布评论

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

评论(1

风和你 2025-02-02 16:10:17

尝试以下操作:

cursor.execute("SELECT userID FROM users WHERE userName IN (%s)", hebrew_string)

try this :

cursor.execute("SELECT userID FROM users WHERE userName IN (%s)", hebrew_string)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文