简体   繁体   English

使用sqlalchemy和pyodbc在MS SQL Server上获取查询的返回行

[英]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.

相关问题 SQLAlchemy PyODBC MS SQL Server少DSN连接 - SQLAlchemy PyODBC MS SQL Server DSN-less connection pyodbc 和 MS SQL Server - “没有结果。 以前的 SQL 不是查询。” - pyodbc & MS SQL Server - “No results. Previous SQL was not a query.” 通过sqlalchemy使用mssql + pyodbc从Linux连接到MS SQL Server时出错 - Error using mssql+pyodbc via sqlalchemy to connect to MS SQL Server from Linux 使用 sqlalchemy 和 pyodbc 连接到 SQL Server 2012 - Connecting to SQL Server 2012 using sqlalchemy and pyodbc [SqlAlchemy][mssql][tls1.2] 如何使用来自 windows 的 pyodbc 为连接到 MS SQL 服务器的 SqlAlchemy 强制执行 TLS 1.2? - [SqlAlchemy][mssql][tls1.2] How to enforce TLS 1.2 for SqlAlchemy connecting to MS SQL Server using pyodbc from windows? SQL Server连接-适用于pyodbc,但不适用于SQLAlchemy - SQL Server connection - Works in pyodbc, but not SQLAlchemy 使用Pyodbc和SQLAlchemy连接到SQL Server - Connecting to SQL server using Pyodbc & SQLAlchemy 如何使用 sqlalchemy+pyodbc 和 MS SQL Server 中的多个数据库为 pandas read_sql 创建 sql alchemy 连接? - How to create sql alchemy connection for pandas read_sql with sqlalchemy+pyodbc and multiple databases in MS SQL Server? 使用pyodbc将Python连接到MS SQL Server - Connect Python to MS SQL Server using pyodbc 编码从pyodbc到MS SQL Server的调用 - Encoding calling from pyodbc to a MS SQL Server
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM