Insert array values

How to write and execute a query that inserts array values ​​using libpqxx?

INSERT INTO exampleTable(exampleArray[3]) VALUES('{1, 2, 3}'); 

This sample code gives me:

 ERROR: syntax error at or near "'" 

What's wrong? In the 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"}}'); 
+8
postgresql
source share
1 answer

You must use a column name without an index to insert an array:

 create table example(arr smallint[]); insert into example(arr) values('{1, 2, 3}'); select * from example; arr --------- {1,2,3} (1 row) 

Use the name of the column with the index to access one element of the table:

 update example set arr[2] = 10; select * from example; arr ---------- {1,10,3} (1 row) 

You can use arr[n] in INSERT , but this is of particular importance. Using this syntax, you can create an array with one element indexed from this number:

 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 whose lower bound is 3:

 select arr[3] from example; arr ----- 1 (1 row) 
+5
source share

All Articles