呼叫JSONB_SET()时Java准备的错误
当我在pgadmin中运行此SQL查询时,它可以正常工作。但是不在Java代码中,SQL字符串看起来像这样:
UPDATE table SET col = jsonb_set(col,'{"some_key"}', '{"items":["banana"]}' ::jsonb)
在调试器中,我可以看到与此Java代码一起生成完全相同的查询,但它会引发错误:
final String sql = "UPDATE table SET col = jsonb_set(col,?, ? ::jsonb) ";
try {
connection = defaultDatabase.getConnection();
stmt = connection.prepareStatement(sql);
stmt.setString(1, keyName);
stmt.setString(2, keyValue);
stmt.execute();
} catch (SQLException e) {
logger.error("error:", e.getMessage());
} finally {
DbUtils.closeQuietly(connection);
DbUtils.closeQuietly(stmt);
}
错误:
ERROR: function jsonb_set(jsonb, character varying, jsonb) does not exist
Hint: No function matches the given name and argument types. You might need to add explicit type casts.
Position: 40
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
正如第三个论点已被投入到JSONB一样,第二个论点应该被施放给文本数组。
?你在里面看到了什么?我看到的是,它与您显示的其他查询并不完全相同,因为参数是单独发送的,并标有其类型。
当您将值插入查询中并使用简单协议(例如PGADMIN显然确实如此)时,它们的类型是未知的,因此数据库决定必须是文本[]以匹配函数签名。但是,当将其明确标记为角色变化时,数据库只是接受该标签,然后未能找到具有该类型签名的相应函数。
Just as the third argument has been cast to JSONB, the 2nd one should be cast to text array.
What debugger? And what do you see in it? What I see is that it is not exactly the same as that other query you show, because the params are sent separately and are labeled with their types.
When you interpolate the values into the query and use the simple protocol, like pgadmin apparently does, their types are unknown and so the database decides it must be text[] to match the function signature. But when it is explicitly labelled as being character varying, then the database just accepts that label, and then fails to find a corresponding function with that type signature.
是
text []
- and array - 不是varchar
或text
。因此,像从PGADMIN中的测试中一样,传递
keyValue
包裹在数组文字中:或
仅需要双引号来包装字符串中的特殊字符。
不是 :
请参阅:
The 2nd argument for
jsonb_set()
istext[]
- an array - notvarchar
ortext
.So pass your
keyValue
wrapped in an array literal like you did in your test from pgAdmin:or
Double quotes are only needed to wrap special characters in the string.
Not:
See: