all Python 3sqlite3term
sqlite3.Row
connections and queries
A result row whose columns are reachable both by index and by name: row['name']. Enable it with conn.row_factory = sqlite3.Row.
Syntax
conn.row_factory = sqlite3.Row; row['column']
Without row_factory a query returns plain tuples, and the code breaks whenever the SELECT column order changes. Column names are matched case-insensitively: row['NAME'] finds the column name. print(row) shows only <sqlite3.Row object ...> — use dict(row) for debugging or JSON; keys() lists the column names. There is no attribute access like row.name, only brackets.
Examples
import sqlite3
conn = sqlite3.connect(':memory:')
conn.row_factory = sqlite3.Row
row = conn.execute("SELECT 1 AS id, 'ann' AS name").fetchone()
print(row['name'], row['NAME'], row[0]) # → ann ann 1
print(row.keys()) # → ['id', 'name']
print(dict(row)) # → {'id': 1, 'name': 'ann'}
conn.close()
import sqlite3
conn = sqlite3.connect(':memory:')
conn.row_factory = sqlite3.Row
row = conn.execute("SELECT 'ann' AS name").fetchone()
try:
print(row.name)
except AttributeError:
print('нет row.name — только row["name"]') # → нет row.name — только row["name"]
conn.close()
See also
Open in the full glossarydocs.python.orgReport an inaccuracy