Currently I am running an sql query through python to extract information from a database.
My code is:
cities = ('London')
sql = 'SELECT * FROM Customers WHERE City IN ({});'
cur.execute(sql, cities)
I now want to put in an additional IN statement so my sql query would look something like this:
coun = ('uk')
cities = ('london')
sql = 'SELECT * FROM Customers WHERE City IN ({}) AND Country IN ({});'
cur.execute(sql, cities, coun)
This code doesn't work because I have specified two variable lists in the execute command but I'm also wondering if SQL doesn't like having two IN statements? I tried to test it separately but it didn't like the two IN statements.
Is there a way I can get this code to work? I'm running python 2.7 and the sqlite3 module. The cities and coun lists may change for each user input so I can't just put the raw values into the SQL statement.
curand have you read the documentation for that class and for theexecutemethod? Why are you declaring your strings within parantheses?('london') == 'london'If you want to create a tuple literal, you need to use a comma('london',)citiesandcounto the appropriate parameter markers.