Skip to main content

How to Read SQLite Columns by Name in Python with sqlite3.Row

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

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​

Syntax
connection.row_factory = sqlite3.Row
row = connection.execute("<SELECT statement>").fetchone()
row["<column name>"]
PartWhat it means
connection.row_factory = sqlite3.RowEvery 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​

row_by_name.py
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()

The row_by_name.py script in a macOS-style code window: it sets row_factory to sqlite3.Row, adds one book, then prints its title and year by column name, the row's keys and the row turned into a dict.

  • connection.row_factory = sqlite3.Row comes right after connect(), before any query.
  • row["title"] and row["year"] read the columns by name.
  • row.keys() lists the column names, and dict(row) builds a dictionary from them.

Step 2: Run It​

Terminal
uv run python row_by_name.py
Output
Rivers of Salt came out in 2023
['title', 'year']
{'title': 'Rivers of Salt', 'year': 2023}

The output in a terminal: Rivers of Salt came out in 2023, the list of keys title and year, and the dict with title Rivers of Salt and 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​

MistakeWhat happensDo this instead
Reading a column as an attribute, row.titleAttributeError: 'sqlite3.Row' object has no attribute 'title'Use row["title"]
A name the query didn't select, row["author"]IndexError: No item with that keySelect the column, or check row.keys()
Setting row_factory on the connection after creating a cursorThat cursor keeps returning tuplesSet it right after connect()

Troubleshooting​

SymptomLikely causeFix
TypeError: tuple indices must be integers or slices, not strrow_factory wasn't set on this connectionAdd 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​