如何使用预准备语句更新数组中的单个元素?

huangapple go评论85阅读模式
英文:

How do I update a single element of an ARRAY with a prepared statement?

问题

我们有一个带有ARRAY列的HSQLDB表格。如何使用低成本的预处理语句来更新单个元素?

到目前为止,我们尝试过:

PreparedStatement pstmt = con.prepareStatement("UPDATE mytbl SET col[?] = ? WHERE id = ?");
...
pstmt.setInt(1, idx);
pstmt.setInt(2, val);
pstmt.setInt(3, id);
...

但是这并不起作用。

英文:

We have a HSQLDB table with an ARRAY column. How can we update a single element with a low-cost prepared statement?

So far, we have tried:

PreparedStatement pstmt = con.prepareStatement("UPDATE mytbl SET col[?] = ? WHERE id = ?");
...
pstmt.setInt(1, idx);
pstmt.setInt(2, val);
pstmt.setInt(3, id);
...

But that does not work.

答案1

得分: 1

数据库引擎需要一个CAST作为提示,以确定参数之一的类型。在这种情况下,请使用数组元素的类型。下面的示例显示了当数组包含INTEGER或TIMESTAMP值时所需的转换:

PreparedStatement pstmt = con.prepareStatement("UPDATE mytbl SET col[?] = CAST(? AS INT) WHERE id = ?");
PreparedStatement pstmt = con.prepareStatement("UPDATE mytbl SET col[?] = CAST(? AS TIMESTAMP) WHERE id = ?");
英文:

The database engine needs a CAST as a hint to determine the type of one of the parameters. Use the type of array elements in the case. The examples below show the required cast when the array contains INTEGER or TIMESTAMP values:

PreparedStatement pstmt = con.prepareStatement("UPDATE mytbl SET col[?] = CAST(? AS INT) WHERE id = ?");
PreparedStatement pstmt = con.prepareStatement("UPDATE mytbl SET col[?] = CAST(? AS TIMESTAMP) WHERE id = ?");

huangapple
  • 本文由 发表于 2020年10月26日 23:44:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/64540422.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定