How to pass variable parameter to "IN clause"

I am preparing session.execute statement like below. I have few conditions and one IN clause. I am getting below error. I know I am making a mistake but cannot get it to work. filter_site_value = ['filter 1', 'filter 2']

session = get_session()
query = 'SELECT * FROM table where cv = %s AND dt > %s and dt < %s AND st IN  (%s)' % ','.join('%s' for i in filter_site_value)
data = (filter_customer_value,filter_date_start_value, filter_date_end_value, filter_site_value)
rows = session.execute(query, data)

      

"errorType": "TypeError", "errorMessage": "Not enough arguments for format string"

Please, help.

+3


source to share


1 answer


your string has 3 places where you need to format the value ( %s

) into it , you only provide one value:','.join('%s' for i in filter_site_value)

So, if you have:

x = string_to_format % values

      

and string_to_format contains the X value %s

(or %d %r ...

), then you need the X values ​​invalues

see: https://docs.python.org/2/library/string.html , https://pyformat.info/



What you probably want to do is:

query = 'SELECT * FROM table where cv = %s AND dt > %s and dt < %s AND st IN  ('+ ','.join(filter_site_value)+')'
data = (filter_customer_value,filter_date_start_value, filter_date_end_value)

      

or

query = 'SELECT * FROM table where cv = %s AND dt > %s and dt < %s AND st IN  ('+ ','.join(%s for i in filter_site_value)+')'
data = (filter_customer_value,filter_date_start_value, filter_date_end_value)+tuple(filter_site_value)

      

+3


source







All Articles