【问题标题】:How can I update varbinary(max) columns in SQL Server with flask?如何使用烧瓶更新 SQL Server 中的 varbinary(max) 列?
【发布时间】:2020-05-04 05:59:58
【问题描述】:

我有一个City 表:

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

我想更新此表的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]'('. (102) (SQLExecDirectW); [42000] [Microsoft][ ODBC SQL Server 驱动程序][SQL Server]无法准备语句。(8180)"

或:

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

但它不起作用。

【问题讨论】:

  • UPDATE City Set pic = ? where id=2 是 SQL 语法。
  • 我不认为 any 数据库使用SET (pic) VALUES (?)。标准语法为SET field=value,即SET Pic=?

标签: sql sql-server flask pyodbc insert-update


【解决方案1】:

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

file = request.files['file']

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

作为说明,我在这里使用了(file.read(),),并带有一个逗号。这是为了确保 Python 将其读取为元组而不是标量变量。

【讨论】:

  • 不工作。我收到此错误:pyodbc.Error: ('HY000', '[HY000] [Microsoft][ODBC SQL Server Driver]Warning: Partial insert/update. The insert/update of a text or image column(s) did not succeed. (0) (SQLPutData); [HY000] [Microsoft][ODBC SQL Server Driver][SQL Server]The text, ntext, or image pointer value conflicts with the column name specified. (7125)')
  • @Tomy 那么看起来您的数据本身还有另一个问题。至少我已经纠正了你的语法问题。
  • 我可以使用您的代码更新图像,以减少 409/kB 的图片。对于超过 409/kB 我收到此错误 pyodbc.Error: ('HY000', '[HY000] [Microsoft][ODBC SQL Server Driver]Warning: Partial insert/update. The insert/update of a text or image column( s) 没有成功。(0) (SQLPutData); [HY000] [Microsoft][ODBC SQL Server Driver][SQL Server]text、ntext 或 image 指针值与指定的列名冲突。(7125)') .但是一个 VARBINARY(MAX) (正常的 2GB 限制)。
  • 我上传了this 并且无法正常工作,但是当我将图片的大小从 70% 调整为更少时,picLess70Percent.
  • 感谢link。我改变. con= pyodbc.connect('DRIVER={SQL Server};SERVER=LENOVO-PCN;DATABASE=testing;') to ===>> con= pyodbc.connect('DRIVER={SQL Server Native Client 11.0};SERVER =LENOVO-PCN;DATABASE=testing;') 我的意思是 db = pyodbc.connect ("Driver={SQL Server Native Client 11.0}. and update any size file is working.
猜你喜欢
  • 1970-01-01
  • 2019-04-30
  • 1970-01-01
  • 1970-01-01
  • 2021-01-31
  • 2014-07-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多