pymssql 问题 - INSERT 不适用于参数
我正在 Linux 上使用 pymssql 通过 Python 进行数据库编程。
我在向查询传递参数时遇到问题。这个问题似乎只存在于 INSERT 查询中。
这可行:
query = "SELECT col1, col2 FROM table WHERE col3=%s"
cur.execute(query, (value,))
但这不行:
query = "INSERT INTO table (col1, col2) VALUES (%s, %s)"
cur.execute(query, (value1, value2,))
知道为什么 INSERT 查询不起作用吗?
这是回溯:
Traceback (most recent call last):
File "test.py", line 46, in ?
cur.execute(query, (value1, value2,))
File "/usr/lib/python2.4/site-packages/pymssql.py", line 126, in execute
self.executemany(operation, (params,))
File "/usr/lib/python2.4/site-packages/pymssql.py", line 152, in executemany
raise DatabaseError, "internal error: %s" % self.__source.errmsg()
pymssql.DatabaseError: internal error: None
I'm using pymssql to do database programming with Python on Linux.
I'm having problems with passing parameters to queries. This problem only seems to exist with INSERT queries.
This works:
query = "SELECT col1, col2 FROM table WHERE col3=%s"
cur.execute(query, (value,))
But this doesn't:
query = "INSERT INTO table (col1, col2) VALUES (%s, %s)"
cur.execute(query, (value1, value2,))
Any idea why the INSERT query won't work?
Here is the traceback:
Traceback (most recent call last):
File "test.py", line 46, in ?
cur.execute(query, (value1, value2,))
File "/usr/lib/python2.4/site-packages/pymssql.py", line 126, in execute
self.executemany(operation, (params,))
File "/usr/lib/python2.4/site-packages/pymssql.py", line 152, in executemany
raise DatabaseError, "internal error: %s" % self.__source.errmsg()
pymssql.DatabaseError: internal error: None
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
结果我试图插入的值之一是 unicode 类型。当我使用
str(value1)
将其转换为字符串时,查询有效。Turns out one of the values I was trying to insert was of type unicode. When I converted it to string, using
str(value1)
, the query worked.这个怎么样:
How about this: