2012-12-27 57 views
1

這個查詢有什麼問題。sql鍊金術標量查詢問題?

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的核心數量

回答

3

不知道爲什麼你需要使用.as_scalar () 方法。此方法旨在用於創建子查詢對象。子查詢對象用於構建另一個查詢,而不是直接執行。

從你的代碼似乎你正在構建的SELECT查詢被執行:

在這種情況下,下面的代碼就足夠了:

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語句,轉換爲標量子查詢。