How to Read SQLite Columns by Name in Python with sqlite3.Row
By default, sqlite3 gives you each row as a tuple, so you read columns by position: row[0], row[1]. That breaks quietly when someone reorders the SELECT. sqlite3.Row lets you write row["title"] instead. This guide reads one book by column name, with real output from Python 3.15.2.
Syntax
connection.row_factory = sqlite3.Row
row = connection.execute("<SELECT statement>").fetchone()
row["<column name>"]
| Part | What it means |
|---|---|
connection.row_factory = sqlite3.Row | Every query on this connection returns Row objects instead of tuples |
row["<column name>"] | The value of that column. row[0] still works too |
Python's sqlite3 documentation: "A Row instance serves as a highly optimized row_factory for Connection objects. It supports iteration, equality testing, len(), and mapping access by column name and index."
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.row_factory = sqlite3.Row
connection.execute("CREATE TABLE books (title TEXT, year INTEGER)")
connection.execute("INSERT INTO books VALUES ('Rivers of Salt', 2023)")
row = connection.execute("SELECT title, year FROM books").fetchone()
print(row["title"], "came out in", row["year"])
print(row.keys())
print(dict(row))
connection.close()

connection.row_factory = sqlite3.Rowcomes right afterconnect(), before any query.row["title"]androw["year"]read the columns by name.row.keys()lists the column names, anddict(row)builds a dictionary from them.
Step 2: Run It
uv run python row_by_name.py
Rivers of Salt came out in 2023
['title', 'year']
{'title': 'Rivers of Salt', 'year': 2023}

The columns came back by name. keys() gave the names in the order of the SELECT, and dict(row) paired each name with its value, ready to print as JSON or pass to a function.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
Reading a column as an attribute, row.title | AttributeError: 'sqlite3.Row' object has no attribute 'title' | Use row["title"] |
A name the query didn't select, row["author"] | IndexError: No item with that key | Select the column, or check row.keys() |
Setting row_factory on the connection after creating a cursor | That cursor keeps returning tuples | Set it right after connect() |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
TypeError: tuple indices must be integers or slices, not str | row_factory wasn't set on this connection | Add connection.row_factory = sqlite3.Row after connect() |
FAQ
Are column names case-sensitive?
No. The documentation says Row "provides indexed and case-insensitive named access to columns", and row["TITLE"] returned the same value as row["title"].
Is sqlite3.Row slower than a tuple?
Barely. The documentation describes it as having "minimal memory overhead and performance impact over a tuple."
Conclusion
With row_factory = sqlite3.Row, the book's columns were readable as row["title"] and row["year"], keys() listed them, and dict(row) turned the row into a dictionary.
Additional Resources
- sqlite3.Row — what a
Rowsupports. - How to create and use row factories —
Rowand custom row factories.
