postgresql 插入数组值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33335338/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Inserting array values
提问by kto? tam
How do I write and execute a query which inserts array values using libpqxx?
如何编写和执行使用 libpqxx 插入数组值的查询?
INSERT INTO exampleTable(exampleArray[3]) VALUES('{1, 2, 3}');
This example code gives me:
这个示例代码给了我:
ERROR: syntax error at or near "'"
What is wrong? In PostgreSQL documentation I found that:
怎么了?在 PostgreSQL 文档中,我发现:
CREATE TABLE sal_emp (
name text,
pay_by_quarter integer[],
schedule text[][]
);
...
...
INSERT INTO sal_emp
VALUES ('Bill',
'{10000, 10000, 10000, 10000}',
'{{"meeting", "lunch"}, {"training", "presentation"}}');
采纳答案by klin
You should use a column name without an index to insert an array:
您应该使用没有索引的列名来插入数组:
create table example(arr smallint[]);
insert into example(arr) values('{1, 2, 3}');
-- alternative syntax
-- insert into example(arr) values(array[1, 2, 3]);
select * from example;
arr
---------
{1,2,3}
(1 row)
Use the column name with an index to access a single element of the array:
使用带有索引的列名来访问数组的单个元素:
select arr[2] as "arr[2]"
from example;
arr[2]
--------
2
(1 row)
update example set arr[2] = 10;
select * from example;
arr
----------
{1,10,3}
(1 row)
You can use arr[n]
in INSERT
but this has special meaning. With this syntax you can create an array with one element indexed from the given number:
您可以使用arr[n]
inINSERT
但这有特殊含义。使用此语法,您可以创建一个数组,其中包含一个从给定数字索引的元素:
delete from example;
insert into example(arr[3]) values (1);
select * from example;
arr
-----------
[3:3]={1}
(1 row)
As a result you have an array which lower bound is 3:
因此,您有一个下限为 3 的数组:
select arr[3] from example;
arr
-----
1
(1 row)