Home > Blockchain >  SqlAlchemy, cannot retrieve the counting of a table rows
SqlAlchemy, cannot retrieve the counting of a table rows

Time:09-19

I am trying to store the number of row of the table "computers" this way:

with my_db.connect() as conn:

    result = conn.execute(text("SELECT COUNT(id) FROM computers;"))

    return result

But when I print "result" I get:

sqlalchemy.engine.cursor.LegacyCursorResult object

Any idea ?

CodePudding user response:

Use the connection's scalar method:

with my_db.connect() as conn:

    result = conn.scalar(text("SELECT COUNT(id) FROM computers;"))

    return result

.scalar will return the first column of the first row of the result.

CodePudding user response:

Try: return result.one() to get one row of values from the result cursor

  • Related