I need to create a dynamic SQL query that changes based on different arguments/variables existence and value:
So, I have something like this:
def create_query(user=None, max=None, list=None)
qs = 'SELECT N.status'
if user:
qs = qs.join('U.email')
qs = join('from notes as N)'
if user:
qs = 'LEFT JOIN users AS U on N.user_id=U.id'
if max:
qs = qs.join('where max=') + max
if list:
qs = qs.join('where fame_values in list')
....
I have around 20 variables that can change the query, so I'm looking for something more pythonic(like list comprehensions with conditions) but for strings
qs.join('where max=') + maxsince this is a security hole and will also impact application scalability. Use a bind variable instead.