Python/psycopg2 WHERE IN statement
To expland on the answer a little and to address named parameters, and converting lists to tuples:
countryList = ['UK', 'France']sql = 'SELECT * from countries WHERE country IN %(countryList)s'cur.execute(sql, { # You can pass a dict for named parameters rather than a tuple. Makes debugging hella easier. 'countryList': tuple(countryList), # Converts the list to a tuple.})
You could use a python list directly as below. It acts like the IN operator in SQL and also handles a blank list without throwing any error.
data=['UK','France']sql='SELECT * from countries WHERE country = ANY (%s)'cur.execute(sql,(data,))
source:http://initd.org/psycopg/docs/usage.html#lists-adaptation