Re: Getting back the primary key after INSERT

Поиск
Список
Период
Сортировка
От hodges@xprt.net
Тема Re: Getting back the primary key after INSERT
Дата
Msg-id 3BD45155.11741.48D478@localhost
обсуждение исходный текст
Ответ на Getting back the primary key after INSERT  ("Scott W. Hill" <shill@verisign.com>)
Список 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@xprt.net or tom_hodges@yahoo.com
ICQ 10149621, YahooMessenger tom_hodges
Mail: 14314 SW Allen Blvd, #317; Beaverton OR 97005 USA

В списке pgsql-novice по дате отправления:

Предыдущее
От: "Scott W. Hill"
Дата:
Сообщение: Getting back the primary key after INSERT
Следующее
От: "Josh Berkus"
Дата:
Сообщение: Re: [PHP] Can't postgres join tables on varchar fields ?