SELECT * in SQLAlchemy?

2020-06-30 11:56发布

Is it possible to do SELECT * in SQLAlchemy?

Specifically, SELECT * WHERE foo=1?

8条回答
smile是对你的礼貌
2楼-- · 2020-06-30 12:28

If you're using the ORM, you can build a query using the normal ORM constructs and then execute it directly to get raw column values:

query = session.query(User).filter_by(name='jack')
for cols in session.connection().execute(query):
    print cols
查看更多
三岁会撩人
3楼-- · 2020-06-30 12:29

Is no one feeling the ORM love of SQLALchemy today? The presented answers correctly describe the lower level interface that SQLAlchemy provides. Just for completeness this is the more-likely (for me) real-world situation where you have a session instance and a User class that is orm mapped to the users table.

for user in session.query(User).filter_by(name='jack'):
     print user
     # ...

And this does an explicit select on all columns.

查看更多
登录 后发表回答