82

How do I write and execute a query which inserts array values using 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:

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"}}');
4
  • What is the type of examplearray? Commented Oct 25, 2015 at 23:13
  • @klin It is smallint but it does not matter Commented Oct 26, 2015 at 6:08
  • Of course this matters. Your query would be syntactically correct on a text array column . Commented Oct 26, 2015 at 10:52
  • @klin I know. I meant, there is no difference between types, which are used. Commented Oct 26, 2015 at 15:17

2 Answers 2

68

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:

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:

select arr[3] from example;
 arr 
-----
   1
(1 row)
Sign up to request clarification or add additional context in comments.

2 Comments

It works, thanks. I tried "[]", "[3]", " ' " and without " ' " but I don't tried without "[]".
also you shall be able to execute this statement INSERT INTO example(arr) VALUES (ARRAY[1,2,3]) (as another alternative to use if you don't have a string value)
38

ref: https://ubiq.co/database-blog/how-to-insert-into-array-in-postgresql/

A. use ARRAY

insert into employees (id, name, phone_numbers)
         values (1, ' John Doe', ARRAY ['9998765432','9991234567']);

// less nested quotes

B. use '{}'

insert into employees (id, name, phone_numbers)
  values (2, ' Jim Doe', '{"9996587432","9891334567"}');

OR

insert into employees (id, name, phone_numbers)
  values (2, ' Jim Doe', '{9996587432,9891334567}');

// seems inner quotes " not necessary,
// number or string depends on column type.

1 Comment

For string arrays it's more readble to use ARRAY

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.