Unicode String不能以Pymssql的形式传递给SQL查询
我很难通过参数将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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试以下操作:
try this :