插入数组值 [英] Inserting array values
本文介绍了插入数组值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何编写和执行使用libpqxx插入数组值的查询?
How do I write and execute a query which inserts array values using libpqxx?
INSERT INTO exampleTable(exampleArray[3]) VALUES('{1, 2, 3}');
此示例代码为我提供:
ERROR: syntax error at or near "'"
什么是错误?在PostgreSQL文档中,我发现:
What is wrong? In PostgreSQL documentation I found that:
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"}}');
推荐答案
您应该使用没有索引的列名来插入数组:
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)
您可以在 INSERT <中使用
arr [n]
/ code>,但这有特殊含义。使用这种语法,您可以创建一个数组,其中的一个元素从给定的数字开始索引。
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:
delete from example;
insert into example(arr[3]) values (1);
select * from example;
arr
-----------
[3:3]={1}
(1 row)
因此,您有一个下界为3的数组:
As a result you have an array which lower bound is 3:
select arr[3] from example;
arr
-----
1
(1 row)
这篇关于插入数组值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文