Skip to main content

How to Get the ID of a Row You Just Inserted in Python with lastrowid

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

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​

Syntax
cursor = connection.execute("INSERT INTO <table> (<columns>) VALUES (<placeholders>)", <values>)
cursor.lastrowid
PartWhat it means
cursorThe cursor that ran the INSERT. connection.execute() returns it
cursor.lastrowidThe 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​

last_row_id.py
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()

The last_row_id.py script in a macOS-style code window: a notes table with an INTEGER PRIMARY KEY id, and a loop that inserts two notes and prints each one's lastrowid.

  • id INTEGER PRIMARY KEY makes id the row's own number, which SQLite fills in when an INSERT leaves it out.
  • The INSERT gives only the text, so SQLite picks the id.
  • cursor.lastrowid reads it back from the cursor that ran the insert.

Step 2: Run It​

Terminal
uv run python last_row_id.py
Output
Saved 'Buy milk' as note 1
Saved 'Call the plumber' as note 2

The output in a terminal: Saved Buy milk as note 1, and 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​

MistakeWhat happensDo this instead
Reading lastrowid after executemany()It isn't set: a new cursor still showed None after inserting two rowsInsert with execute() when you need each id
Expecting None after a SELECTA cursor that ran only a SELECT showed 0Read lastrowid straight after your INSERT

Troubleshooting​

SymptomLikely causeFix
lastrowid is NoneThe 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​