Skip to content
Advertisement

Passing parameters from Python to SQL Server

I have code like this:

conn = pyodbc.connect(<Connection Details>)
c = conn.cursor()

employee_id=(100,101)
query = "select * from employees where employeeid in ?"

c.execute(query,employee_id)

I am getting this error:

‘The SQL contains 1 parameter markers, but 2 parameters were supplied’, ‘HY000’

Is there any way to pass this parameter? I don’t want to create a dynamic array by concatenation.

Is there any way to name the parameter marker inside the query in case of several where conditions?

Advertisement

Answer

If I remember correctly, the placeholder is %s and not ?.

Regardless, you can use the format method / string formatting to get the job done:

conn = pyodbc.connect(<Connection Details>)
c = conn.cursor()
employee_id=(100,101)

query = "select * from employees where employee_id in {}"
c.execute(query.format(employee_id))
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement