How to Get All Rows of a Query in Python with sqlite3 fetchall()
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
rows = connection.execute("<SELECT statement>").fetchall()
| Part | What it means |
|---|---|
connection.execute("<SELECT statement>") | Runs the query and returns a cursor |
.fetchall() | Reads every remaining row from that cursor |
rows | A 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
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 first query matches both books, and
fetchall()puts them inrows. len(rows)counts them:rowsis an ordinary list.- The second query asks for books after 2030, and there are none.
Step 2: Run It
uv run python fetch_all.py
[('The Glass Orchard', 2019), ('Rivers of Salt', 2023)]
2 books
[]

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
| Mistake | What happens | Do this instead |
|---|---|---|
Calling fetchall() twice on the same cursor | The second call returns []: the first one already read every row | Keep the list from the first call |
| Expecting plain values from a one-column query | Each 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 None | Check if not rows: |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
fetchall() returns [] but the table has rows | The 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
- Cursor.fetchall — reading all remaining rows.
- Cursor.fetchone — reading one row at a time.
