Re: Getting back the primary key after INSERT

From: hodges(at)xprt(dot)net
To: "Scott W(dot) Hill" <shill(at)verisign(dot)com>, pgsql-novice(at)postgresql(dot)org
Subject: Re: Getting back the primary key after INSERT
Date: 2001-10-23 00:03:17
Message-ID: 3BD45155.11741.48D478@localhost
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-novice

Scott,
I am pretty new at this also but here is what I used.
After adding a record to table persons I need to get the latest
value from its sequence file, persons_id_seq, and use it to
link a new record in table addresses.

/* Get the ID value for the volunteer just added to persons table
We will use this as the index for the addresses table */
$personidsql = "select last_value from persons_id_seq";
$result = pg_Exec($GLOBALS["localhost"], $personidsql);
$myrow = pg_fetch_row ($result,0); echo "person id",$myrow[0],"<BR>";

Now key value is in $myrow[0].

/* Add Mailing Address */
if ($maddress1 != "") {
$address_type = "mailing";
$sqladdress = "insert into addresses
(person_id,address_type, address1, address2, city, state_code,
province,
country_code, postalcode, remarks)
values
('$myrow[0]','$address_type','$maddress1','$maddress2','$mcity',
'$mstate_code','$mprovince','$mcountry_code',
'$mpostalcode','$remarks')";

Cheers, Tom

On 22 Oct 2001, at 13:40, Scott W. Hill wrote:

> I'm pretty new with JDBC and Postgres. Here's my question:
>
> I have a table with an automatically incrementing key. I'm inserting
> things into the table via an INSERT statement from JDBC. Once I do the
> INSERT, I want to know what new number was assigned by the database to
> the row that I just inserted. Any ideas?
>
> --Scott
Tom Hodges, hodges(at)xprt(dot)net or tom_hodges(at)yahoo(dot)com
ICQ 10149621, YahooMessenger tom_hodges
Mail: 14314 SW Allen Blvd, #317; Beaverton OR 97005 USA

In response to

Browse pgsql-novice by date

  From Date Subject
Next Message Oliver Elphick 2001-10-23 04:12:00 Re: Problem with createdb & case (7.1.3)
Previous Message Josh Berkus 2001-10-22 23:09:20 Re: Getting back the primary key after INSERT