How to Get One Row of a Query in Python with sqlite3 fetchone()
Many queries look one thing up: a book by its year, a customer by their email, an order by its number. For those, fetchone() reads a single row, and tells you with None when there's nothing to read. This guide finds one book, then looks for one that isn't there, with real output from Python 3.15.2.
Syntax
row = connection.execute("<SELECT statement>").fetchone()
if row is None:
<nothing matched>
| Part | What it means |
|---|---|
.fetchone() | Reads the next row of the cursor |
row | A tuple with the row's columns, or None when there are no more rows |
Python's sqlite3 documentation: "If row_factory is None, return the next row query result set as a tuple." And: "Return None if no more data is available."
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 books (title TEXT, year INTEGER)")
connection.execute("INSERT INTO books VALUES ('The Glass Orchard', 2019), ('Rivers of Salt', 2023)")
row = connection.execute("SELECT title, year FROM books WHERE year = 2023").fetchone()
print(row)
title, year = row
print(title, "came out in", year)
missing = connection.execute("SELECT title, year FROM books WHERE year = 1999").fetchone()
print(missing)
connection.close()

- The first query matches one book, and
fetchone()returns it as a tuple. title, year = rowunpacks the tuple into two variables, in the order theSELECTnamed the columns.- The second query matches nothing.
Step 2: Run It
uv run python fetch_one.py
('Rivers of Salt', 2023)
Rivers of Salt came out in 2023
None

The 2023 book came back as one tuple, and unpacking gave its title and year. The 1999 lookup returned None, not an error and not an empty tuple. That's why real code checks for None before using the row.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
Unpacking the result without checking for None | TypeError: cannot unpack non-iterable NoneType object when nothing matched | Check if row is None: first |
Expecting fetchone() to return the only row of a query that matches several | It returns the first row; the next call returns the second | Make the WHERE clause match one row, or use fetchall() |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
TypeError: cannot unpack non-iterable NoneType object | The query matched no row | Handle the None case before unpacking |
FAQ
What does a second fetchone() return?
The next row, if there is one. After a query with one matching row, the first call returned the row and the second returned None.
How do I check whether a row exists?
Run the query and test the result: exists = row is not None.
Conclusion
fetchone() returned the 2023 book as a tuple ready to unpack, and None for the year with no book, which is the case your code has to handle.
Additional Resources
- Cursor.fetchone — reading the next row.
- Cursor.fetchall — reading all remaining rows.
