1

I have a dict like:

{'a': 1, 'b':2, ... }

and an sql alchemy expression like

s = select([some columns]\
.where(put here my dict with a==1 and b==2 and ...)\
.group_by(*[some columns])

but I do not find the correct syntax for the "where" part.

What is the correct way to do this?

2 Answers 2

4

Convert your conditions dict to an iterable and unpack it in and_ operator.

from sqlalchemy import and_
from sqlalchemy.sql import column

conditions = {'a': 1, 'b':2}
filters = [column(key) == value for key, value in conditions.items()]

s = select([some columns].where(and_(*filters)).group_by(*[some columns])
Sign up to request clarification or add additional context in comments.

Comments

0

In my case data select from table using two where clause

def get_trade_images_by_trade(self,a,b):
    stmt = select([self.table_name]).where(self.table_name.c.column_name.in_([a]))
    stmt = stmt.where(self.table_name.c.column_name.in_([b]))    
    print(stmt)
    result = engine.execute(stmt)
    return result 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.