How to Get the ID of a Row You Just Inserted in Python with lastrowid
When SQLite numbers new rows for you, your program often needs that number right away: to show "Saved as note 2", or to link an order's items to the order. cursor.lastrowid gives it to you after the insert. This guide saves two notes and prints each one's id, with real output from Python 3.15.2.
Syntax
cursor = connection.execute("INSERT INTO <table> (<columns>) VALUES (<placeholders>)", <values>)
cursor.lastrowid
| Part | What it means |
|---|---|
cursor | The cursor that ran the INSERT. connection.execute() returns it |
cursor.lastrowid | The row id of the row that INSERT added |
Python's sqlite3 documentation: "Read-only attribute that provides the row id of the last inserted row."
Prerequisites
- uv and Python. This guide uses uv 0.13.10 with Python 3.15.2, in a project made with
uv init.
Step 1: Write the Script
import sqlite3
connection = sqlite3.connect(":memory:")
connection.execute("CREATE TABLE notes (id INTEGER PRIMARY KEY, text TEXT)")
for text in ["Buy milk", "Call the plumber"]:
cursor = connection.execute("INSERT INTO notes (text) VALUES (?)", (text,))
print(f"Saved {text!r} as note {cursor.lastrowid}")
connection.close()

id INTEGER PRIMARY KEYmakesidthe row's own number, which SQLite fills in when anINSERTleaves it out.- The
INSERTgives only the text, so SQLite picks theid. cursor.lastrowidreads it back from the cursor that ran the insert.
Step 2: Run It
uv run python last_row_id.py
Saved 'Buy milk' as note 1
Saved 'Call the plumber' as note 2

SQLite numbered the notes 1 and 2, and each lastrowid read the number of the row its own INSERT had just added.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
Reading lastrowid after executemany() | It isn't set: a new cursor still showed None after inserting two rows | Insert with execute() when you need each id |
Expecting None after a SELECT | A cursor that ran only a SELECT showed 0 | Read lastrowid straight after your INSERT |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
lastrowid is None | The cursor hasn't run an INSERT with execute() | Read it from the cursor that ran the INSERT |
FAQ
What does lastrowid hold after other statements?
Python's documentation says it changes only after an INSERT or REPLACE. On Python 3.15.2 it behaved differently: a new cursor showed None, and after a SELECT it showed 0. Once another cursor had inserted row 1, a SELECT on the first cursor showed 1. Every execute() copies SQLite's last inserted row id, which belongs to the whole connection, not to one cursor. The sqlite3 maintainer describes the 0 in CPython issue #90419. The safe habit: read lastrowid right after your own INSERT.
Can SQL give me the id instead?
Yes. SQLite's RETURNING clause returns it as a row: connection.execute("INSERT INTO notes (text) VALUES (?) RETURNING id", (text,)).fetchone() gave (2,) for a second note.
Conclusion
After each INSERT, cursor.lastrowid held the id SQLite had given the new note: 1, then 2.
Additional Resources
- Cursor.lastrowid — the attribute and its rules.
- CPython issue #90419 — the maintainer's notes on
lastrowidafter other statements.
