Re: portable DBAPI auto-increment

From: Federico Di Gregorio <federico(dot)digregorio(at)dndg(dot)it>
To: psycopg(at)postgresql(dot)org
Cc: sienkiew(at)stsci(dot)edu
Subject: Re: portable DBAPI auto-increment
Date: 2011-04-08 07:51:20
Message-ID: 4D9EBE78.3080005@dndg.it
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: 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(dot)digregorio(at)dndg(dot)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

In response to

Browse psycopg by date

  From Date Subject
Next Message Karsten Hilbert 2011-04-08 09:52:13 Re: portable DBAPI auto-increment
Previous Message Mark Sienkiewicz 2011-04-07 20:40:29 portable DBAPI auto-increment