Search code examples
pythonmysql-connector-python

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?


Solution

  • 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)