【问题标题】:Python3 insert jsonb array into Postgresql 9.5Python3 将 jsonb 数组插入 Postgresql 9.5
【发布时间】:2020-07-02 09:43:09
【问题描述】:

这里真的需要一些帮助。我在 Postgresql 9.5 中有一个 jsonb[] 列。 假设我在 Python 中有一个名为 history 的列表。它看起来像这样 [{'name': 'A'},{'name': 'B'},{'name': 'C'}]。使用 json.dumps(history) 之后。它看起来像 [{"name": "A"},{"name": "B"},{"name": "C"}]。

我的sql是:sql = ("INSERT INTO tableA (history_column) VALUES (%s, %s) WHERE tableA.id = %s") connection.cursor().execute(sql, json.dumps(history), id)

我在这里遇到的问题是我无法插入json.dumps(history)。 Postgresql 显示错误消息:

`'[{"name":...

“[”必须引入明确指定的数组维度。`

我认为 json.dumps 不知何故不能很好地与 jsonb 数组配合使用。不知道有没有办法解决这个问题。

谢谢!

【问题讨论】:

    标签: sql python-3.x postgresql jsonb postgresql-9.5


    【解决方案1】:

    回答我自己的问题。潜在的解决方案是编写一个 python 脚本将查询转换为update tableName set columnName = ARRAY['{"name":"A"}'::jsonb, '{"name": "A"}'::jsonb] where id = %s; 这可能不是一个好的解决方案,但现在可以。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-11-02
      • 1970-01-01
      • 1970-01-01
      • 2020-11-11
      • 2020-06-28
      • 1970-01-01
      • 1970-01-01
      • 2018-12-10
      相关资源
      最近更新 更多