Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to execute LIKE query in sqlalchemy?

I am using sqlalchemy to fetch data from tables. Right now fetching all records from a table called audit_trail_table is working as expected.

select_stmt = select([self.audit_trail_table]).where(self.audit_trail_table.c.id == int(id))
row = conn.execute(select_stmt).fetchone()

Now I wish to execute LIKE queries on multiple columns as follows:

filter_query = #(some value which will work as a filter_query while fetching )records
filter_stmt = select([self.audit_trail_table]).where(self.audit_trail_table.c.first_name like '%' + filter_query + '%' or self.audit_trail_table.c.last_name like '%')

But it gives an error at LIKE. How can I achieve this?

like image 730
exAres Avatar asked Jul 18 '14 10:07

exAres


1 Answers

You can use the like operator:

select([self.audit_trail_table]).where(
    self.audit_trail_table.c.first_name.like('%' + filter_query + '%') or 
    self.audit_trail_table.c.last_name.like('%abc%')
)
like image 65
Nilesh Avatar answered Oct 15 '22 04:10

Nilesh