开发者

Selecting specific columns with generative selects

开发者 https://www.devze.com 2023-03-21 15:05 出处:网络
I\'m learning SQL alchemy and going through the Expression Language Tutorial. I really like the generative selects because I can use table 开发者_C百科reflection and then easily query the table with T

I'm learning SQL alchemy and going through the Expression Language Tutorial. I really like the generative selects because I can use table 开发者_C百科reflection and then easily query the table with Table class methods. This is what I'm doing now to do what I want.

from sqlalchemy import Table, create_engine, MetaData
engine = create_engine('mysql://...')
meta.bind = engine
table = Table('footable', meta, autoload=True)

result = table.select().where(...).execute()

I've written many selects before and I always select the specific columns that I need rather than selecting all. Is there a way to specify which columns to return in my SQL alchemy select?


Read more on the select() in the documentation, especially in respect to first two parameters.
But the following should be the right direction for you to work:

from sqlalchemy.sql import select, and_, or_, not_
# ...
query = select(# what to select (tables or columns)
               [table.c.column1, table.c.column2],
               # filters (use any expression using and_, or_, not_...
               and_(table.c.column1.like("j%")),
               )
result = query.execute()
0

精彩评论

暂无评论...
验证码 换一张
取 消