必须使用 pyodbc UPDATE 查询声明标量变量“@Station”

Must declare the scalar variable "@Station" with pyodbc UPDATE query

Station = SN[0]
Asset = AN[0]
y = ASD[0]
z = ACD[0]

cursor = conn.cursor()
cursor.execute('SELECT * FROM station')
cursor.execute('''
    UPDATE station
    SET ActualStartDate = y, ActualCompletionDate = z
    WHERE StationName = @Station and AssetNumber = @Asset
    ''')
conn.commit()

我收到这个错误:

('42000', '[42000] [Microsoft][ODBC SQL Server Driver][SQL Server]Must declare the scalar variable "@Station". (137) (SQLExecDirectW)')

我想从上面的 4 个 numpy 数组 SN[0]、AN[0]、SCD[0] SSD[0] 中获取值,并在 UPDATE 查询中使用这些值。

这个可以执行吗?

如 pyodbc documentation 中所述,您的参数占位符必须是问号,并且您必须在 .execute() 调用中将参数值与 SQL 命令文本一起传递:

sql = """\
UPDATE station
SET ActualStartDate = ?, ActualCompletionDate = ?
WHERE StationName = ? and AssetNumber = ?
"""
params = (y, z, Station, Asset)
cursor.execute(sql, params)