Python3将jsonb数组插入Postgresql 9.5

时间:2018-07-12 00:47:53

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

这里确实需要一些帮助。我在Postgresql 9.5中有一列作为jsonb []。 假设我有一个名为Python的历史记录的列表。看起来像这样[{'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”:...

“ [”必须引入明确指定的数组尺寸。

enter image description here

我认为json.dumps不能与jsonb数组一起很好地工作。我不知道是否有解决此问题的方法。

谢谢!

1 个答案:

答案 0 :(得分:0)

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