SQLAlchemy 使用包含 @DECLARE 本地表的原始 SQL 执行

发布于 2025-01-05 01:15:09 字数 990 浏览 5 评论 0原文

我被困住了——我有以下带有 SQL alchemy 的 python 脚本,我已经非常成功地将其用于其他几个目的。

import sqlalchemy
from sqlalchemy import MetaData
from sqlalchemy.orm import *

engine = sqlalchemy.create_engine("this line of code would provide credentials to the     database")
connection = engine.connect()
session = sessionmaker(bind=engine)
result = connection.execute(sqlquery)

for row in result: print row

最近,我发现如果我的 'sqlquery' 包含 @Declare MyTable 语句,我会收到错误:

"This result object does not return rows. "
sqlalchemy.exc.ResourceClosedError: This result object does not return rows. It has been closed automatically.

这是我的 SQL 查询,它在 SSMS 中工作正常,但当我尝试使用 SQLAlchemy 执行它时不会执行

DECLARE @USER TABLE
(
    UserID INT
    , StatsVals INT
)

INSERT INTO @USER (UserID, StatsVals)
    SELECT TOP 10 u.UserID
    , u.StatsVals
    FROM UserProfile u

SELECT * FROM @USER

有谁知道为什么SQLAlchemy 会给我这个错误吗?我应该怎么做才能解决这个问题?

I'm stuck -- I have the following python script with SQL alchemy which I've been using quite successfully for several other purposes.

import sqlalchemy
from sqlalchemy import MetaData
from sqlalchemy.orm import *

engine = sqlalchemy.create_engine("this line of code would provide credentials to the     database")
connection = engine.connect()
session = sessionmaker(bind=engine)
result = connection.execute(sqlquery)

for row in result: print row

Recently though I discovered that if my 'sqlquery' contains an @Declare MyTable statement I get the error:

"This result object does not return rows. "
sqlalchemy.exc.ResourceClosedError: This result object does not return rows. It has been closed automatically.

Here is my SQL query which works fine in SSMS but will not execute when I try to execute it using SQLAlchemy

DECLARE @USER TABLE
(
    UserID INT
    , StatsVals INT
)

INSERT INTO @USER (UserID, StatsVals)
    SELECT TOP 10 u.UserID
    , u.StatsVals
    FROM UserProfile u

SELECT * FROM @USER

Does anyone know why SQLAlchemy would be giving me this error? What should I do to fix this?

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

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

发布评论

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

评论(2

吃兔兔 2025-01-12 01:15:09

当 DBAPI 在游标上执行时,如果存在结果,则需要存在一个名为 cursor.description 的属性。如果不是,SQLAlchemy 知道没有结果可返回。

在这种情况下,这可能是 DBAPI 的问题,除非这种用法属于游标上“多个结果集”的范围。 SQLAlchemy 到目前为止还没有直接支持多个结果集。如果是这种情况,您需要直接使用 DBAPI 游标并调用 .nextset() 来获取结果。您可以通过以下方式获得此信息:(

connection = engine.raw_connection()
cursor = connection.cursor()

关于cursor.nextset()如何工作的文档,位于http:// /www.python.org/dev/peps/pep-0249/

否则,您确实需要联系 DBAPI 作者,看看您在这里所做的是否真的可行。我猜这是 pyodbc,即使您没有指定您所在的后端。如果是这样,您可以通过 http://code.google.com/p/pyodbc/。

When the DBAPI executes on a cursor, if results are present, there's an attribute called cursor.description that's required to be present. If it's not, SQLAlchemy knows there's no results to return.

In this case, this is probably an issue with the DBAPI, unless this usage falls under the realm of "multiple result sets" on the cursor. SQLAlchemy doesn't have direct support for multiple result sets as of yet. If this is the case, you'd need to use the DBAPI cursor directly and call .nextset() to get at the results. You can get this via:

connection = engine.raw_connection()
cursor = connection.cursor()

(docs on how cursor.nextset() works at http://www.python.org/dev/peps/pep-0249/)

Otherwise, you'd really need to contact the DBAPI author and see if what you're doing here is really possible. I'm guessing this is pyodbc, even though you haven't specified what backend you're on. If so, you can contact them at http://code.google.com/p/pyodbc/.

陌上芳菲 2025-01-12 01:15:09

要更具体地了解 zzzeek 答案,您应该执行类似的操作,

from pyodbc import ProgrammingError
from sqlalchemy import create_engine

# do the connection
engine = create_engine('mssql+pyodbc://user:password@SERVERNAME/DatabaseName?driver=SQL Server')
connection = engine.raw_connection()
cursor = connection.cursor()

# do the query
cursor.execute(query)

# processing it
while True:
    try:
        result = cursor.fetchall()

        # ... do something with result

        if not cursor.nextset(): # trying to get next set
            break
    except ProgrammingError as ex:
        pass

这有助于我使用具有大量时态表的非常复杂的 MSSQL 并声明

To be more specific about zzzeek answer you should do something like that

from pyodbc import ProgrammingError
from sqlalchemy import create_engine

# do the connection
engine = create_engine('mssql+pyodbc://user:password@SERVERNAME/DatabaseName?driver=SQL Server')
connection = engine.raw_connection()
cursor = connection.cursor()

# do the query
cursor.execute(query)

# processing it
while True:
    try:
        result = cursor.fetchall()

        # ... do something with result

        if not cursor.nextset(): # trying to get next set
            break
    except ProgrammingError as ex:
        pass

That helps me to work with very complex MSSQL with lots of temporal tables and declares

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文