Re: portable DBAPI auto-increment - Mailing list psycopg

From Federico Di Gregorio
Subject Re: portable DBAPI auto-increment
Date
Msg-id 4D9EBE78.3080005@dndg.it
Whole thread Raw
In response to portable DBAPI auto-increment  (Mark Sienkiewicz <sienkiew@stsci.edu>)
List psycopg
On 07/04/11 22:40, Mark Sienkiewicz wrote:
> Hi,
>
> I have a portability issue that I'm not sure what to do with:  I need an
> automatically unique column, but I don't see a portable way to ask for
> it in my code.
>
> For my example, I'm going to use column k as the key and v as the value,
> though in real life there are another 10 or so columns.
>
> In sqlite and mysql, I can do
>    c.execute("insert into foo ( v ) values ( 'xyz' )")
>    k = c.lastrowid

Declare k as "serial" and then do the following:

c.execute("INSERT INTO foo (v) VALUES ('xyz') RETURNING k")
k = c.fetchone()[0]

Hope this helps,
federico

--
Federico Di Gregorio                         federico.digregorio@dndg.it
Studio Associato Di Nunzio e Di Gregorio                  http://dndg.it
  We should forget about small efficiencies, say about 97% of the
   time: premature optimization is the root of all evil.    -- D.E.Knuth

psycopg by date:

Previous
From: Mark Sienkiewicz
Date:
Subject: portable DBAPI auto-increment
Next
From: Karsten Hilbert
Date:
Subject: Re: portable DBAPI auto-increment