Getting COUNT from sqlalchemy

前端 未结 3 879
闹比i
闹比i 2020-12-11 06:02

I have:

res = db.engine.execute(\'select count(id) from sometable\')

The returned object is sqlalchemy.engine.result.ResultProxy

3条回答
  •  感情败类
    2020-12-11 06:21

    While the other answers work, SQLAlchemy provides a shortcut for scalar queries as ResultProxy.scalar():

    count = db.engine.execute('select count(id) from sometable').scalar()
    

    scalar() fetches the first column of the first row and closes the result set, or returns None if no row is present. There's also Query.scalar(), if using the Query API.

提交回复
热议问题