[issue46249] [sqlite3] move set lastrowid out of the query loop

Erlend E. Aasland report at bugs.python.org
Tue Jan 4 15:02:26 EST 2022


Erlend E. Aasland <erlend.aasland at innova.no> added the comment:

> If possible, it's usually better to have the .executemany() create a
> cursor with an output array providing the row ids, e.g. using "INSERT ...
> RETURNING ..." (PostgreSQL). That way you can access all row ids and
> can also provide the needed detail in case the INSERTs happen out of
> order to map them to the input data.

Hm, maybe so. But it will add a lot of overhead and complexity to executemany(), and there haven't been requests for this feature for sqlite3. AFAIK, there hasn't been request for lastrowid for executemany() at all. OTOH, my proposal of modifying lastrowid to always show the rowid of the actual last inserted row is a very cheap operation, _and_ it simplifies the code (=> increased maintainability), so I think I'll go for that.

> For cases where you don't need sequence IDs, it's often better to
> not rely on auto-increment columns for IDs, but instead use random
> pre-generated IDs. Saves roundtrips to the database and works nicely
> with cluster databases as well.

Yes, but in those cases you keep track of the row id yourself, so you probably won't need lastrowid ;)

----------

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue46249>
_______________________________________


More information about the Python-bugs-list mailing list