有谁知道如何在不遍历结果集的情况下从 SQL Alchemy 查询 ResultProxy 对象中获取行数?ResultProxy.rowcount 属性显示 0,我希望它的值为 2。对于更新,它显示受影响的行数,这是我所期望的。
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
engine = create_engine(
'oracle+cx_oracle://user:pass@host:port/database'
)
session = sessionmaker(
bind = engine
, autocommit = False
, autoflush = False
)()
sql_text = u"""
SELECT 1 AS Val FROM dual UNION ALL
SELECT 2 AS Val FROM dual
"""
results = session.execute(sql_text)
print '%s rows returned by query...\n' % results.rowcount
print results.keys()
for i in results:
print repr(i)
输出:
0 rows returned by query...
[u'val']
(1,)
(2,)