Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8002727
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T16:26:57+00:00 2026-06-04T16:26:57+00:00

I am using SQLAlchemy in Python, and I want to know how to get

  • 0

I am using SQLAlchemy in Python, and I want to know how to get the total number of rows in a column. I have variables defined:

engine = sqlalchemy.create_engine(url, ehco=False)
Session = sqlalchemy.orm.sessionmaker(bind=engine)
Session = session()
metadata = sqlalchemy.MetaData(engine)
Base = declarative_base(metadata=metadata)

# A class representing the shape_congress_districts_2012 table
class Congress(Base):
    __tablename__ = 'shape_congress_districts_2012'
    id = geoalchemy.Column(sqlalchemy.Integer, primary_key=True)
    name = geoalchemy.Column(sqlalchemy.Unicode)
    geom = geoalchemy.GeometryColumn(geoalchemy.Polygon(2))
    geom_simple = geoalchemy.GeometryColumn(geoalchemy.Polygon(2))
    area = geoalchemy.Column(sqlalchemy.Float)
    state_id = geoalchemy.Column(sqlalchemy.Integer)
    census_year = geoalchemy.Column(sqlalchemy.Date)

geoalchemy.GeometryDDL(Congress.__table__)

I want to determine the total number of rows in the table without having to wait a whole bunch of time querying the database. Currently, I have a bit of code:

rows = session.query(Congress).all()

Then I can access them from list, but this requires me to load everything into memory at once.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T16:27:00+00:00Added an answer on June 4, 2026 at 4:27 pm

    This should work

    rows = session.query(Congress).count()
    

    EDIT: Another way related to my first try

    from sqlalchemy import func
    rows = session.query(func.count(Congress.id)).scalar()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using SQLalchemy for a Python project, and I want to have a tidy
I'm using the SQLAlchemy Python ORM in a Pylons project. I have a class
I've created a simple DB using SQLAlchemy in python and there is a column
Im using the python framework flask with sqlalchemy. My many-to-many looks like this: collections_questions
Hi im using sqlalchemy on a db2 table with 500k rows. using plain sql
I've been trying to map an object to a database using SQLAlchemy but have
I'm going to have two independent programs (using SqlAlchemy / ORM / Declarative) that
I've been using Python for most of my web projects lately, and have come
I am using SqlAlchemy, a python ORM library. And I used to access database
I'm looking for the simplest way of using python and SQLAlchemy to produce some

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.