X-Original-To: pgsql-jdbc-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 3099D52A9A for ; Sat, 12 Mar 2005 22:25:20 +0000 (GMT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 79949-06 for ; Sat, 12 Mar 2005 22:25:11 +0000 (GMT) Received: from Firewall.ev.co.yu (gateway.ev.co.yu [194.247.214.130]) by svr1.postgresql.org (Postfix) with SMTP id BBDBA52A52 for ; Sat, 12 Mar 2005 22:25:06 +0000 (GMT) Received: by Firewall.ev.co.yu; (5.65v4.0/1.3/10May95) id AA08392; Fri, 12 Mar 1999 23:29:30 +0100 Received: from [192.168.61.197] (Dial-In-1.ev.co.yu [192.168.61.197]) by Uprava.ev.co.yu (8.13.1/8.13.1) with ESMTP id j2CMMrAo011977 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO) for ; Sat, 12 Mar 2005 23:22:56 +0100 (MET) Message-Id: <42336C35.7080003@ev.co.yu> Date: Sat, 12 Mar 2005 23:24:53 +0100 From: Nikola Milutinovic User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.5) Gecko/20041217 X-Accept-Language: en-us, en Mime-Version: 1.0 To: pgsql-jdbc@postgresql.org Subject: How to get automatically generated "id" field Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: by amavisd-new X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0 tagged_above=0 required=5 tests= X-Spam-Level: X-Archive-Number: 200503/130 X-Sequence-Number: 12522 Hi all. This may be a bit more for general ML, but I'm in a fix and need an advice. I have several tables with auto-generated "id" field, like this. CREATE TABLE photo ( id SERIAL PRIMARY KEY, size INT8, file VARCHAR(256) ) I would like to be able to insert values and get back the "id" of a newly inserted record. What is a good way to do it? I imagine I could open a transaction and read the value of the sequence, but that is ugly, since the sequence name is autogenerated and PG specific. Would it be better to change "SERIAL" to just INT8 and do in a transaction: ...start transaction executeQuery( "SELECT max( id )+1 AS new_id FROM photo" ); ..read "new_id" executeUpdate( "INSERT INTO photo (id, size, file) VALUES (...)" ); ..commit Nix.