user4718221
user4718221

Reputation: 606

SQLAlchemy getting a class object instead of actual result

I am running a query on an existing Postgres database using automap.

Base = automap_base()

Base.prepare(engine, reflect=True)

City = Base.classes.city

dm = session.query(City).first()

Output: <sqlalchemy.ext.automap.city at 0x15f7a87b8>

How do I set it up so I can see an actual result from the table?

Upvotes: 1

Views: 474

Answers (1)

Gord Thompson
Gord Thompson

Reputation: 123849

From comments:

I have used a dict to see the actual results and then turned it into a pandas df. Is there a standard way in dealing with this?

Just use pandas' read_sql_query() method:

Base = automap_base()
Base.prepare(autoload_with=engine)
City = Base.classes.city

query = select(City)
df = pd.read_sql_query(query, engine)
print(df)
"""
   id  city_name prov
0   1    Calgary   AB
1   2  Vancouver   BC
"""

Upvotes: 1

Related Questions