Skip to main content

How to Get All Rows of a Query in Python with sqlite3 fetchall()

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

Looping over a cursor reads rows one at a time. When you want them all at once, as a list you can count, sort or pass on, call fetchall(). This guide reads two books into a list and shows what a query with no matches returns, with real output from Python 3.15.2.

Syntax​

Syntax
rows = connection.execute("<SELECT statement>").fetchall()
PartWhat it means
connection.execute("<SELECT statement>")Runs the query and returns a cursor
.fetchall()Reads every remaining row from that cursor
rowsA list, one tuple per row

Python's sqlite3 documentation: "Return all (remaining) rows of a query result as a list. Return an empty list if no rows are 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_all.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)")

rows = connection.execute("SELECT title, year FROM books ORDER BY year").fetchall()
print(rows)
print(len(rows), "books")

none_found = connection.execute("SELECT title FROM books WHERE year > 2030").fetchall()
print(none_found)

connection.close()

The fetch_all.py script in a macOS-style code window: it creates a books table with two books, reads them with fetchall into rows, prints the list and its length, then runs a query that matches nothing and prints its fetchall result.

  • The first query matches both books, and fetchall() puts them in rows.
  • len(rows) counts them: rows is an ordinary list.
  • The second query asks for books after 2030, and there are none.

Step 2: Run It​

Terminal
uv run python fetch_all.py
Output
[('The Glass Orchard', 2019), ('Rivers of Salt', 2023)]
2 books
[]

The output in a terminal: a list with two tuples, The Glass Orchard 2019 and Rivers of Salt 2023, then 2 books, then an empty list.

The first fetchall() returned a list of two tuples, and len() counted 2. The query with no matches didn't fail and didn't return None: it returned an empty list, [], so code that loops over the result simply does nothing.

Common Mistakes​

MistakeWhat happensDo this instead
Calling fetchall() twice on the same cursorThe second call returns []: the first one already read every rowKeep the list from the first call
Expecting plain values from a one-column queryEach row is still a tuple: [('A',), ('B',), ('C',)]Take the first item of each row, such as [row[0] for row in rows]
Checking rows is None to find "no results"fetchall() never returns NoneCheck if not rows:

Troubleshooting​

SymptomLikely causeFix
fetchall() returns [] but the table has rowsThe cursor was already read, by a loop or an earlier fetchall()Run the query again

FAQ​

Is fetchall() a good idea for a huge table?​

It reads every row into memory at once. For a large result, loop over the cursor instead, which reads one row at a time.

What does "remaining" mean in the documentation?​

Rows already read from the cursor, by fetchone() or a loop, aren't returned again: fetchall() returns the rest.

Conclusion​

fetchall() turned the query's two books into a list of tuples that len() could count, and a query with no matches into an empty list.

Additional Resources​