如何使用 Flask 更新 SQL 服务器中的 varbinary(max) 列?

How can I update varbinary(max) columns in SQL Server with flask?

我有一个 City table:

name | id | pic
-----+----+-----
A    | 1  | null
B    | 2  | null
C    | 3  | null
D    | 4  | null

我想更新此 table 的 pic 列,其中 id 是 2。我该怎么做?

我的代码是:

file = request.files['file']
saveFileToDB = "UPDATE [Db2].[dbo].[City] SET (pic) VALUES (?) WHERE id = 2"
CU.execute(saveFileToDB, (file.read()))
CU.commit()

错误信息:

pyodbc.ProgrammingError: ('42000', "[42000] [Microsoft][ODBC SQL Server Driver][SQL Server]Incorrect syntax near '('. (102) (SQLExecDirectW); [42000] [Microsoft][ODBC SQL Server Driver][SQL Server]Statement(s) could not be prepared. (8180)"

或:

saveFileToDB = "UPDATE [Db2].[dbo].[City] SET pic = Convert(Varbinary(max),'%s') WHERE id = 2"%file.read()

但它不起作用。

您的更新语法已关闭。使用此版本:

file = request.files['file']

saveFileToDB = "UPDATE City SET pic = ? WHERE id = 2"
CU.execute(saveFileToDB, (file.read(),))
CU.commit()

请注意,我使用了 (file.read(),),此处带有尾随逗号。这是为了确保 Python 将其作为元组而不是标量变量读取。