Skip to main content

How to Get One Row of a Query in Python with sqlite3 fetchone()

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

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​

Syntax
row = connection.execute("<SELECT statement>").fetchone()
if row is None:
<nothing matched>
PartWhat it means
.fetchone()Reads the next row of the cursor
rowA 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​

fetch_one.py
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 fetch_one.py script in a macOS-style code window: it creates a books table with two books, reads the 2023 book with fetchone, unpacks it into title and year, then looks for a 1999 book and prints the result.

  • The first query matches one book, and fetchone() returns it as a tuple.
  • title, year = row unpacks the tuple into two variables, in the order the SELECT named the columns.
  • The second query matches nothing.

Step 2: Run It​

Terminal
uv run python fetch_one.py
Output
('Rivers of Salt', 2023)
Rivers of Salt came out in 2023
None

The output in a terminal: the tuple Rivers of Salt 2023, the sentence Rivers of Salt came out in 2023, then 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​

MistakeWhat happensDo this instead
Unpacking the result without checking for NoneTypeError: cannot unpack non-iterable NoneType object when nothing matchedCheck if row is None: first
Expecting fetchone() to return the only row of a query that matches severalIt returns the first row; the next call returns the secondMake the WHERE clause match one row, or use fetchall()

Troubleshooting​

SymptomLikely causeFix
TypeError: cannot unpack non-iterable NoneType objectThe query matched no rowHandle 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​