mysql solution

Chris Angelico rosuav at gmail.com
Thu Jan 24 12:29:05 CET 2013


On Thu, Jan 24, 2013 at 10:25 PM, Lele Gaifax <lele at metapensiero.it> wrote:
> The simplest way is to execute a SELECT just after the insertion, doing
> a
>
>   SELECT pin FROM counters WHERE page = %s
>
> I don't use MySQL, so I can't say if it supports "INSERT ... RETURNING ..."
> SQL syntax: should it, then you could insert the data and fetch
> the pin in one shot, with something like
>
>   INSERT INTO counters (page, hits) VALUES (%s, %s) RETURNING (pin)

AFAIK it doesn't, but if pin is an AUTO_INCREMENT primary key, you can
retrieve the ID of the newly inserted record. It's not nearly as
flexible as INSERT... RETURNING, but it covers the most common use
case.

ChrisA



More information about the Python-list mailing list