Hi,
I'm trying to insert values into the Postgres database and I can't figure out the correct grammar for the "ID" field.

My ID field type is int4 and default value is, nextval('wsckl_id_seq'::text), which will automatically increment the value by 1.
(postgres sort of came up with this by itself).

My current insert string is:
$result=pg_exec($conn,"insert into wsckl values ('nextval('"wsckl_id_seq"'::text)','$arr[1]','$arr[2]','$arr[3]','','')");

and it screams parse error :o(

If I change this -- 'nextval('"wsckl_id_seq"'::text -- into nothing or numeric value, everything works fine, except the ID value gets out of wack. Which I prefer not to do.

Thank You for your assistance in advance.

Petri

    This is error in PHP code. Your double quots are not ecaped:

    $result=pg_exec($conn,"insert into wsckl values ('nextval('\"wsckl_id_seq\"'::text)','$arr[1]','$arr[2]','$arr[3]','','')");

      Still don't accept :o(

      I get different error now.

      ERROR: parser: parse error at or near """

        • [deleted]

        Why don't you just specify which columns you do want to put values into, and let PostgreSQL fill in the sequence field by itself?

          vincent wrote:

          Why don't you just specify which columns you do want to put values into, and let PostgreSQL fill in the sequence field by itself?

          I will try after I find an example how to do that?
          I learn by studying examples.

          Thanks,
          Petri

            Enough bumping head to the 17" glass at front of you will eventually bring the answer. :o)

            Through countless trial-and-error loops it finally worked out using following string.

            $result=pg_exec($conn, "insert into wsckl values (nextval('wsckl_id_seq'::text),'$arr[1]','$arr[2]','$arr[3]')");

            Time to rest....

            Petri

              • [deleted]

              lol nice one.

              Actually, the database should fill in the next value in the sequence by itself if you don't specify a value for it.

              So if your table is: id|name|phone, and id has a sequence assigned to it, you could just do

              INSERT INTO table (name, phone) VALUES ('john','123');
              and the database will use the sequence to generate the value for 'id'.

                Write a Reply...