我使用PYODBC根据一个dataframe列的值多次查询SQL DB(如下所示为一个值列表,因为我使用了ToList()函数将该列转换为一个列表)
#the connection string
cnxn = pyodbc.connect(driver='{SQL Server}', server = 'NameOfTheServer',autocommit = True,uid ='name',pwd ='password')
cursor = cnxn.cursor()
params = ['3122145', '523532236']
sql = ("""
SELECT SO.column
FROM table AS SO
WHERE SO.column = ?
""")
cursor.executemany(sql, params)
row = cursor.fetchone()
即使我正在使用列表,executemany函数也会引发错误: TypeError:(“参数必须位于列表、元组或行”“HY000”“中)
.executemany
不用于SELECT语句。如果我们尝试,我们只能返回最后一行:相反,我们需要构建一个参数占位符字符串,并在in子句中使用它,如下所示:
相关问题 更多 >
编程相关推荐