3 ms·
How do you parameterize something like 'WHERE foo in (a, b, c, ...)' where a,b,c... is a variable length list of parameters?
by boring_twenties 7y ago
How do you parameterize something like 'WHERE foo in (a, b, c, ...)' where a,b,c... is a variable length list of parameters?
- andor 7y ago1) dynamically adjust the number of parameters in your prepared statement params = ", ".join(["?"] * count) template = "SELECT ... WHERE foo in (%s)" % (params) # proceed to prepare a statement from the query template 2) use a subquery, insert data somewhere if necessary SELECT ... WHERE foo in (SELECT foo FROM foos WHERE some_value = 42) 3) if the maximum number of parameters is low, you can keep it variable by enabling parts of the query with boolean parameters: SELECT ... WHERE (test_param1 AND foo = param1) OR (test_param2 AND foo = param2) OR (test_param3 AND foo = param3)