[Tutor] OT - SQL methodology.

Kent Johnson kent37 at tds.net
Tue Mar 22 12:00:16 CET 2005

Liam Clarke wrote:
> Hi, 
> This is a SQL query for the advanced db gurus among you (I'm looking at Kent...)

Uh oh, you're in trouble if you think I'm an "advanced db guru" :-)
> After I've run an insert statement, should I get the new primary key
> (it's autoincrementing) by using PySQLite's cursor.lastrowid in a
> select statement, or is there a more SQLish way to do this?

AFAIK there is no standard SQL way to do this, it is database-dependent. Python DB-API provides a 
portable interface using lastrowid; I would use that.

You don't have to do another select, lastrowid is an attribute of the cursor itself. It calls the 
SQLite function sqlite_last_insert_rowid().

> In the SQL books I've got, they always seem to have an optional select
> statement on the end of inserts/updates, and I was thinking maybe I
> could do it that way also, but I can't figure out a logical way of
> putting
> 'select primary_key from foo where primary_key value > every other
> primary_key value'

Use cursor.lastrowid, that's what it is for.


More information about the Tutor mailing list