Author jim_minter
Recipients jim_minter
Date 2013-01-04.17:24:19
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1357320260.32.0.764505068506.issue16864@psf.upfronthosting.co.za>
In-reply-to
Content
sqlite3 doesn't populate the lastrowid member of the Cursor object when a SQL REPLACE statement is executed.

The following snippet doesn't work as I would expect:
cursor = db.execute("REPLACE INTO table(column) VALUES ('datum')")
print cursor.lastrowid  # prints None

The following snippet, with SQL which is in effect identical to SQLite, does work as expected:
cursor = db.execute("INSERT OR REPLACE INTO table(column) VALUES ('datum')")
print cursor.lastrowid  # prints some rowid

Looking at Modules/_sqlite/cursor.c, in _pysqlite_query_execute(), the following snippet is found:

if (!multiple && statement_type == STATEMENT_INSERT) {
    Py_BEGIN_ALLOW_THREADS
    lastrowid = sqlite3_last_insert_rowid(self->connection->db);
    Py_END_ALLOW_THREADS
    self->lastrowid = PyLong_FromLong((long)lastrowid);
} else {
    Py_INCREF(Py_None);
    self->lastrowid = Py_None;
}

I suggest this should read something like:
if (!multiple && (statement_type == STATEMENT_INSERT || statement_type == STATEMENT_REPLACE)) {
instead of:
if (!multiple && statement_type == STATEMENT_INSERT) {

Thanks,

Jim
History
Date User Action Args
2013-01-04 17:24:20jim_mintersetrecipients: + jim_minter
2013-01-04 17:24:20jim_mintersetmessageid: <1357320260.32.0.764505068506.issue16864@psf.upfronthosting.co.za>
2013-01-04 17:24:20jim_minterlinkissue16864 messages
2013-01-04 17:24:19jim_mintercreate