Re: FAQ error

From: Bruce Momjian <pgman(at)candle(dot)pha(dot)pa(dot)us>
To: Brent Verner <brent(at)rcfile(dot)org>
Cc: PostgreSQL-development <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: FAQ error
Date: 2001-10-13 03:52:43
Message-ID: 200110130352.f9D3qh301243@candle.pha.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

> On 10 Oct 2001 at 17:12 (-0400), Bruce Momjian wrote:
> |
> | Our FAQ, item 4.16.2 has:
> |
> | $newSerialID = nextval('person_id_seq');
> | INSERT INTO person (id, name) VALUES ($newSerialID, 'Blaise Pascal');
> |
> | Is this correct Perl? I don't see a nextval() function in Perl. Can
> | you call SQL server-side functions natively from Perl?
>
> no. The proper perl code would be more like...
>
> use DBI;
> my ($lastid,$nextid,$sql,$rv);
> my $dbh = DBI->connect("perldoc DBD::Pg");
>
> # to use the nextval
> $sql = "SELECT nextval('person_id_seq')";
> $nextid = ($dbh->selectrow_array($sql))[0];
> $sql = "INSERT INTO person (id, name) VALUES ($nextid, 'Blaise Pascal');
> $rv = $dbh->do($sql);

OK, new FAQ code is:

$sql = "SELECT nextval('person_id_seq')";
$newSerialID = ($conn->selectrow_array($sql))[0];
INSERT INTO person (id, name) VALUES ($newSerialID, 'Blaise Pascal');
$res = $dbh->do($sql);
>
> # or to get the currval
> $sql = "INSERT INTO person (name) VALUES ('Blaise Pascal');
> $rv = $dbh->do($sql);
> $sql = "SELECT currval('person_id_seq')";
> $lastid = ($dbh->selectrow_array($sql))[0];

and:

INSERT INTO person (name) VALUES ('Blaise Pascal');
$res = $conn->do($sql);
$sql = "SELECT currval('person_id_seq')";
$newSerialID = ($conn->selectrow_array($sql))[0];

--
Bruce Momjian | http://candle.pha.pa.us
pgman(at)candle(dot)pha(dot)pa(dot)us | (610) 853-3000
+ If your life is a hard drive, | 830 Blythe Avenue
+ Christ can be your backup. | Drexel Hill, Pennsylvania 19026

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Tom Lane 2001-10-13 03:56:09 Re: TOAST and TEXT
Previous Message Hannu Krosing 2001-10-13 03:52:31 Re: optimizer question