First Check
Commit to Help
Example Code
from sqlmodel import Field, Session, SQLModel, create_engine, select
class Hero(SQLModel, table=True):
id: int | None = Field(default=None, primary_key=True)
name: str
sqlite_file_name = "database.db"
sqlite_url = f"sqlite:///{sqlite_file_name}"
engine = create_engine(sqlite_url, echo=True)
def create_db_and_tables():
SQLModel.metadata.create_all(engine)
def create_heroes():
hero_1 = Hero(name="Deadpond")
hero_2 = Hero(name="Spider-Boy")
hero_3 = Hero(name="Rusty-Man")
with Session(engine) as session: #
session.add(hero_1) #
session.add(hero_2)
session.add(hero_3)
session.commit()
row_counts = session.exec(select(Hero)).count() # HERE! How can I get the row counts? In my code I had where as well.
print(f"row_counts: {row_counts}")
def main():
create_db_and_tables()
create_heroes()
if __name__ == "__main__":
main()
Description
- How can I get the count or row counts of the query?
Operating System
Linux
Operating System Details
Ubuntu 21.10
SQLModel Version
0.0.6
Python Version
3.10.2
Additional Context
None
First Check
Commit to Help
Example Code
Description
Operating System
Linux
Operating System Details
Ubuntu 21.10
SQLModel Version
0.0.6
Python Version
3.10.2
Additional Context
None