此查询出了什么问题。
self.connection.execute(select([func.count(table.c.id)]).as_scalar())
这给了我这个追溯
StatementError: Not an executable clause (original cause: ArgumentError:
Not an executable clause) '(SELECT count(table.id) AS count_1 \nFROM agent)'
或如何使用 SqlAlchemy Core
获取表格中所有行的数量答案 0 :(得分:3)
不确定为什么需要使用.as_scalar()方法。此方法旨在用于创建子查询对象。子查询对象用于构建另一个查询,而不是直接执行。
从您的代码中,您似乎正在构建要执行的选择查询:
在这种情况下,以下代码就足够了:
self.connection.execute(select([func.count(table.c.id)]))
以下代码无例外地工作:
r = engine.execute(select([func.count(table.c.id)]))
print r
for i in r:
print i
结果:
<sqlalchemy.engine.base.ResultProxy object at 0x032F4E30>
(1,)
<强> PS:强> 从sqlalchemy文档: The Query Object: as_scalar
as_scalar()
返回由此表示的完整SELECT语句 查询,转换为标量子查询。