I would recommend a different approach to parameterizing your IN
query. Instead of making a string like "(534,523,544)"
with all of the values you want to check, pass each value as a parameter.
For example:
order_ids = [534, 523, 544]
query = "SELECT * FROM abcDB.zyzTable WHERE ordId IN ("
query += ",".join(["%s"]*len(order_ids)) + ")"
cur.execute(query, order_ids)
Which would effectively execute this query:
"SELECT * FROM abcDB.zyzTable WHERE ordId IN (534,523,544)"
The key here is that I used ",".join(["%s"]*len(order_ids))
to dynamically create the appropriate number of placeholders for my query.
Also note that the second argument to cur.execute()
is the list of order IDs. I did not format the string, as you were doing.
Don't use %
, or you're susceptible to sql-injection. This way is safer, and more reliable. More reading on this post:
Python best practice and securest to connect to MySQL and execute queries.