我正在努力将JSON对象存储到Postgresql数组中..我做了类似的事情 -
CREATE TABLE rock_band
(
name text,
members VARCHAR(200) []
);
INSERT INTO rock_band VALUES ('Linkin Park','{"Name":"Chester" , "age" : "38"}');
ERROR: malformed array literal:
关于我们如何将json存储到Postgresql数组然后将更多json对象附加到同一数组的任何指导/指示。
此外,如果我们可以在Postgresql数组中存储JSON对象,那么我们是否可以存储HSTORE值。
---更新---
好的,在heapanalytics的其中一个人的帮助下,我能够将值作为JSON对象的数组插入
CREATE TABLE rock_band (
name TEXT,
members JSON[] <<--- This is changed now as compared to last one
);
INSERT INTO rock_band VALUES ('The Who', '{}');
要附加到我执行的数组:
UPDATE rock_band SET members = array_append(members, '{"name":"Roger Daltry", "age":61}') WHERE name = 'The Who';
然后我得到这样的东西 -
select * from rock_band;
name | members
---------+----------------------------------------------------------------------------------
The Who | {"{\"name\":\"Roger Daltry\", \"age\":61}","{\"name\":\"Chester\", \"age\":38}"}
(1 row)
但现在的问题是如何查询它,比如 -
test=# select members from rock_band where 'Chester' = ANY(members->'name');
ERROR: operator does not exist: json[] -> unknown
LINE 1: ...mbers from rock_band where 'Chester' = ANY(members->'name');
^
HINT: No operator matches the given name and argument type(s). You might need to add explicit type casts.
有什么建议吗?
答案 0 :(得分:0)
我没有真正看到问题,这完全在文档中 这是一个例子:http://sqlfiddle.com/#!15/864bf/7
E.g。
SELECT name FROM (SELECT name, members, generate_subscripts (members, 1) AS s FROM rock_band) AS foo
WHERE members[s]->>'Name' = 'Chester';