Skip to content
Advertisement

SQL IN operator using pyodbc and SQL Server

I’m using pyodbc to query to an SQL Server database

but am receiving the error below. Does the tuple parameter need to be handled in a different way? Is there a better way to structure this query?

UPDATE:

I was able to get this query to work using the string formatting operator, which isn’t ideal as it introduces security concerns.

Advertisement

Answer

You cannot parameterize multiple values in an IN () clause using a single string parameter. The only way to accomplish that is:

  1. String substitution (as you did).

  2. Build a parameterized query in the form IN (?, ?, . . ., ?) and then pass in a separate parameter for each place holder. I’m not an expert at Python to ODBC but I imagine that this is particularly easy to do in a language like Python. This is safer because you get the full value of parameterization.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement