【问题标题】:Postgres query - update field in json column with text typePostgres 查询 - 使用文本类型更新 json 列中的字段
【发布时间】:2020-03-18 01:14:17
【问题描述】:

我的 postgres 数据库中有一个名为 metadata 的列,它存储一个 JSON 字符串,类型为 TEXT

我正在尝试运行查询以更新 JSON 中名为 myCount 的字段。我正在使用 Spring Boot 和 JDBC。

String query = "UPDATE " + mTableName + " SET metadata = jsonb_set(metadata::jsonb, '{myCount}', ?)::text" +
                " WHERE scope = ?";
        PreparedStatement preparedStmt = jdbcTemplate.getDataSource().getConnection().prepareStatement(query);
        preparedStmt.setInt   (1, myCount);
        preparedStmt.setString(2, scope);

        // execute the java preparedstatement
        return preparedStmt.executeUpdate();

我收到以下错误:ERROR: function jsonb_set(jsonb, unknown, integer) does not

知道如何运行更新 JSON 中的 myCount 列的查询吗?

【问题讨论】:

  • @a_horse_with_no_name 仍然存在问题 - `错误:函数 jsonb_set(jsonb, text[], integer) 不存在`,关于 jsonb,因为内存数据库不支持测试中的 json
  • 因为内存数据库不支持测试中的 json”,这意味着您将永远无法测试您是否在数据库中存储了有效的 JSON。我也很确定这个内存数据库也不支持jsonb_set(),那么你将如何测试这段代码?
  • @a_horse_with_no_name 9.5.9
  • @a_horse_with_no_name 让我们专注于这个问题,当列为json(function jsonb_set(jsonb, text[], integer) does not exist) 时我有同样的错误

标签: java postgresql jdbc


【解决方案1】:

函数 jsonb_set(jsonb, unknown, integer) 没有

告诉您您正在尝试使用整数值作为最后一个参数来调用该函数。但函数定义为jsonb_set(jsonb, text[], jsonb),因此您需要将整数值转换为 JSONB 值:

SET metadata = jsonb_set(metadata::jsonb, '{myCount}'::text[], to_jsonb(?))::text" 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-07
    • 1970-01-01
    • 2020-08-13
    • 1970-01-01
    • 1970-01-01
    • 2023-03-03
    • 2014-02-02
    • 2021-09-27
    相关资源
    最近更新 更多