[英]Fetching a returned row of a query on MS SQL Server with sqlalchemy and pyodbc
I am trying to get the returned row from executing a row insertion. 我试图通过执行行插入来获取返回的行。 I tested the compiled sql directly on SQL Server, the query works fine and it returns a row correctly.
我直接在SQL Server上测试了已编译的sql,查询工作正常,并且正确返回了一行。
The problem is I am not able to get the returned row from the query. 问题是我无法从查询中获取返回的行。 Here is my code and I am using pyodbc driver on Windows.
这是我的代码,我在Windows上使用pyodbc驱动程序。
sql = """
INSERT INTO mytable (id, name)
OUTPUT Inserted.id, Inserted.name
VALUES (...)
"""
result = db.execute(sql).fetchall()
This is the error I am getting: 这是我得到的错误:
ERROR: Failed to save events: (Error) ('HY010', '[HY010] [Microsoft][ODBC SQL Server Driver]Function sequence error (0) (SQLFetch)') None None
Traceback (most recent call last):
File "...\site-packages\sqlalchemy\engine\result.py", line 782, in fetchall
l = self.process_rows(self._fetchall_impl())
File "...\site-packages\sqlalchemy\engine\result.py", line 749, in _fetchall_impl
return self.cursor.fetchall()
pyodbc.Error: ('HY010', '[HY010] [Microsoft][ODBC SQL Server Driver]Function sequence error (0) (SQLFetch)')
Any idea? 任何想法? Thanks in advance!
提前致谢!
You are not executing a query. 您没有执行查询。 You are writing to the database which should not return results.
您正在写入不应返回结果的数据库。 Instead, try the following:
相反,请尝试以下操作:
# assuming db is your cursor
db.execute(sql) # inserts the data
result = db.execute('SELECT * FROM mytable').fetchall()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.