SQLAlchemy 使用包含 @DECLARE 本地表的原始 SQL 执行
我被困住了——我有以下带有 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当 DBAPI 在游标上执行时,如果存在结果,则需要存在一个名为
cursor.description
的属性。如果不是,SQLAlchemy 知道没有结果可返回。在这种情况下,这可能是 DBAPI 的问题,除非这种用法属于游标上“多个结果集”的范围。 SQLAlchemy 到目前为止还没有直接支持多个结果集。如果是这种情况,您需要直接使用 DBAPI 游标并调用
.nextset()
来获取结果。您可以通过以下方式获得此信息:(关于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:(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/.
要更具体地了解 zzzeek 答案,您应该执行类似的操作,
这有助于我使用具有大量时态表的非常复杂的 MSSQL 并声明
To be more specific about zzzeek answer you should do something like that
That helps me to work with very complex MSSQL with lots of temporal tables and declares