SQL IN operator using pyodbc and SQL Server

前端 未结 4 748
时光取名叫无心
时光取名叫无心 2020-12-28 19:53

I\'m using pyodbc to query to an SQL Server database

import datetime
import pyodbc    
conn = pyodbc.connect(\"Driver={SQL Server};Server=\'dbserver\',Datab         


        
4条回答
  •  太阳男子
    2020-12-28 20:38

    To expand on Larry's second option - dynamically creating a parameterized string, I used the following successfully:

    placeholders = ",".join("?" * len(code_list))
    sql = "delete from dbo.Results where RESULT_ID = ? AND CODE IN (%s)" % placeholders
    params = [result_id]
    params.extend(code_list)
    cursor.execute(sql, params)
    

    Gives the following SQL with the appropriate parameters:

    delete from dbo.Results where RESULT_ID = ? AND CODE IN (?,?,?)
    

提交回复
热议问题