This issue tracker will soon become read-only and move to GitHub.
For a smoother transition, remember to log in and link your GitHub username to your profile.
For more information, see this post about the migration.

classification
Title: [doc] fix inaccuracies in sqlite3.Cursor.lastrowid docs
Type: Stage: resolved
Components: Documentation Versions: Python 3.11, Python 3.10, Python 3.9
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: docs@python Nosy List: docs@python, erlendaasland, lemburg, miss-islington, taleinat
Priority: normal Keywords: patch

Created on 2022-01-04 21:46 by erlendaasland, last changed 2022-01-08 20:07 by taleinat. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 30407 merged erlendaasland, 2022-01-04 21:57
PR 30487 merged miss-islington, 2022-01-08 19:17
PR 30488 merged miss-islington, 2022-01-08 19:17
Messages (8)
msg409713 - (view) Author: Erlend E. Aasland (erlendaasland) * (Python triager) Date: 2022-01-04 21:46
The sqlite3 docs say that lastrowid is set to None after executemany(), or after execute()'ing statements that are not INSERTs or REPLACEs. This is not true; in those cases, lastrowid is preserved. lastrowid is only None in a pristine cursor. Suggesting to reword the docs so they harmonise with the implementation.

Note, there is a quirk, or bug with lastrowid: it is set to 0 if the first statement that's execute()d on a cursor is a non-INSERT/REPLACE statement. For example:

    >>> import sqlite3
    >>> cx = sqlite3.connect(":memory:")
    >>> cu = cx.cursor()
    >>> cu.lastrowid
    >>> cu.execute("select 1")
    >>> cu.lastrowid
    0

This behaviour is consistent across current main through 3.7 (though in 2.7, the behaviour is consistent with the docs, so it probably changed a long time ago). I'm not sure it's worth noting this side effect in the docs though.
msg409714 - (view) Author: Erlend E. Aasland (erlendaasland) * (Python triager) Date: 2022-01-04 21:49
Oh, and the docs says that lastrowid "provides the rowid of the last modified row". This is not true; it provides the row id of the last _inserted_ row.
msg409715 - (view) Author: Erlend E. Aasland (erlendaasland) * (Python triager) Date: 2022-01-04 21:52
I also suggest to add a note about tables without rowids, quoting the SQLite docs:

    Inserts into WITHOUT ROWID tables are not recorded.
msg409716 - (view) Author: Erlend E. Aasland (erlendaasland) * (Python triager) Date: 2022-01-04 22:24
FYI: https://sqlite.org/c3ref/last_insert_rowid.html
msg410106 - (view) Author: Tal Einat (taleinat) * (Python committer) Date: 2022-01-08 19:17
New changeset b6aa38f1ca79600f2ab46ac114ff36461a19c4a3 by Erlend Egeberg Aasland in branch 'main':
bpo-46261: Update `sqlite3.Cursor.lastrowid` docs (GH-30407)
https://github.com/python/cpython/commit/b6aa38f1ca79600f2ab46ac114ff36461a19c4a3
msg410109 - (view) Author: Tal Einat (taleinat) * (Python committer) Date: 2022-01-08 20:05
New changeset 987fba102e909229dd2aa1a6115aa28d514c1818 by Miss Islington (bot) in branch '3.10':
bpo-46261: Update `sqlite3.Cursor.lastrowid` docs (GH-30407)
https://github.com/python/cpython/commit/987fba102e909229dd2aa1a6115aa28d514c1818
msg410110 - (view) Author: Tal Einat (taleinat) * (Python committer) Date: 2022-01-08 20:06
New changeset b29aa71090e4dd34900660ecca8bb62667440f41 by Miss Islington (bot) in branch '3.9':
[3.9] bpo-46261: Update `sqlite3.Cursor.lastrowid` docs (GH-30407)
https://github.com/python/cpython/commit/b29aa71090e4dd34900660ecca8bb62667440f41
msg410111 - (view) Author: Tal Einat (taleinat) * (Python committer) Date: 2022-01-08 20:07
Thanks for this, Erlend!
History
Date User Action Args
2022-01-08 20:07:55taleinatsetstage: patch review -> resolved
2022-01-08 20:07:15taleinatsetmessages: + msg410111
stage: resolved -> patch review
2022-01-08 20:06:51erlendaaslandsetstatus: open -> closed
resolution: fixed
stage: patch review -> resolved
2022-01-08 20:06:17taleinatsetmessages: + msg410110
2022-01-08 20:05:52taleinatsetmessages: + msg410109
2022-01-08 19:17:34taleinatsetnosy: + taleinat
messages: + msg410106
2022-01-08 19:17:22miss-islingtonsetpull_requests: + pull_request28691
2022-01-08 19:17:18miss-islingtonsetnosy: + miss-islington
pull_requests: + pull_request28690
2022-01-04 22:24:52erlendaaslandsetmessages: + msg409716
2022-01-04 21:57:14erlendaaslandsetkeywords: + patch
stage: patch review
pull_requests: + pull_request28613
2022-01-04 21:52:25erlendaaslandsetmessages: + msg409715
2022-01-04 21:49:12erlendaaslandsetmessages: + msg409714
2022-01-04 21:46:55erlendaaslandcreate