kto*_*tam 26 arrays postgresql
如何编写和执行使用libpqxx插入数组值的查询?
INSERT INTO exampleTable(exampleArray[3]) VALUES('{1, 2, 3}');
Run Code Online (Sandbox Code Playgroud)
这个示例代码给了我:
ERROR: syntax error at or near "'"
Run Code Online (Sandbox Code Playgroud)
怎么了?在PostgreSQL文档中,我发现:
CREATE TABLE sal_emp (
name text,
pay_by_quarter integer[],
schedule text[][]
);
Run Code Online (Sandbox Code Playgroud)
...
INSERT INTO sal_emp
VALUES ('Bill',
'{10000, 10000, 10000, 10000}',
'{{"meeting", "lunch"}, {"training", "presentation"}}');
Run Code Online (Sandbox Code Playgroud)
yur*_*hen 27
参考:https://ubiq.co/database-blog/how-to-insert-into-array-in-postgresql/
ARRAYinsert into employees (id, name, phone_numbers)
values (1, ' John Doe', ARRAY ['9998765432','9991234567']);
Run Code Online (Sandbox Code Playgroud)
// 减少嵌套引号
'{}'insert into employees (id, name, phone_numbers)
values (2, ' Jim Doe', '{"9996587432","9891334567"}');
Run Code Online (Sandbox Code Playgroud)
或者
insert into employees (id, name, phone_numbers)
values (2, ' Jim Doe', '{9996587432,9891334567}');
Run Code Online (Sandbox Code Playgroud)
"// 似乎不需要内部引号,
// 数字或字符串取决于列类型。
kli*_*lin 22
您应该使用没有索引的列名来插入数组:
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)
Run Code Online (Sandbox Code Playgroud)
使用带有索引的列名来访问数组的单个元素:
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)
Run Code Online (Sandbox Code Playgroud)
你可以使用arr[n],INSERT但这有特殊意义.使用此语法,您可以创建一个数组,其中一个元素从给定数字索引:
delete from example;
insert into example(arr[3]) values (1);
select * from example;
arr
-----------
[3:3]={1}
(1 row)
Run Code Online (Sandbox Code Playgroud)
结果你有一个下限为3的数组:
select arr[3] from example;
arr
-----
1
(1 row)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
36788 次 |
| 最近记录: |