在Postgres中将条目插入JSON列 [英] Inserting an entry to JSON column in postgres
问题描述
我有一个这样的JSON条目,我需要将其输入一列(称为值)
I have a JSON entry like this, which I need to enter into a column (called values)
["Price Descending","Price Ascending","Name Ascending","Date Descending"]
我该如何将其输入到json列中,我正在尝试的是此操作(我正在使用CodeIgniter)
How can I enter this into a json column , what I am trying is this (I am using CodeIgniter)
$this->db->query("
INSERT INTO tag_sets (type, value) VALUES
('sorting_options', "
'Price Descending', 'Price Ascending', 'Name Ascending',
'Date Descending'";
");
这给我一个错误,是什么插入json列的正确方法是什么?
This gives me an error, what is the correct way to insert into a json column?
推荐答案
如果 value
是 json
列,您可以直接转换数组的文本表示形式。
If value
is a json
column, you can convert the text representation of the array directly.
INSERT INTO tag_sets (type, value)
VALUES (
'sorting_options',
'["Price Descending","Price Ascending","Name Ascending","Date Descending"]'::json
);
您还可以根据情况使用一种方法来生成数组。
类似于 json_build_array
的东西。
可以找到更多的 json
方法此处。
You could also use a method to generate an array, depending on your situation.
Something like json_build_array
.
More json
methods can be found here.
这篇关于在Postgres中将条目插入JSON列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!