Getting COUNT from sqlalchemy
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.
what you are asking for called unpacking, ResultProxy
is an iterable, so we can do
# there will be single record
record, = db.engine.execute('select count(id) from sometable')
# this record consist of single value
count, = record