Skip to content
Advertisement

mysql-connector-python query with WHERE IN

I am trying to query a table with WHERE IN condition using mysql-connector-python like this:

ids = (12, 31)
cursor.execute("SELECT object_name FROM object_table WHERE object_id IN %s", ids)

And I get the following error:

mysql.connector.errors.ProgrammingError: Not all parameters were used in the SQL statement

How can I get it working?

Advertisement

Answer

You should add “” in ids to be a String and also ‘,’ should be replaced with ‘%’

E.g.

ids = "(12, 31)"
cursor.execute("SELECT object_name FROM object_table WHERE object_id IN %s" % ids)
Advertisement