The sqlite3.Row class is supposed to give access to row results by name as well as index. So in some respects it behaves like a dictionary (it even has a keys() method) but it doesn't appear to handle the in operator as one might expect.
The following minimal script illustrates:
import sqlite3
CREATION = 'CREATE TABLE lookup (name TEXT NOT NULL, value TEXT NOT NULL)'
INSERTION = 'INSERT INTO lookup (name, value) VALUES(?, ?)'
SELECTION = 'SELECT name, value from lookup'
db = sqlite3.connect(':memory:')
cur = db.cursor()
cur.execute(CREATION)
cur.execute(INSERTION, ('foo', 'bar'))
db.commit()
cur.row_factory = sqlite3.Row
cur.execute(SELECTION)
rows = cur.fetchall()
row = rows[0]
print(f'{row.keys()=}')
for k in row.keys():
print(f'{k=}')
print(f'{k in row=}')
print(f'{row[k]=}')
prints
row.keys()=['name', 'value']
k='name'
k in row=False
row[k]='foo'
k='value'
k in row=False
row[k]='bar'
One would reasonably expect k in row to be True.
Tested on Python 3.10.0, Ubuntu 20.04, but not believed to be restricted to those.
Linked PRs
The
sqlite3.Rowclass is supposed to give access to row results by name as well as index. So in some respects it behaves like a dictionary (it even has akeys()method) but it doesn't appear to handle theinoperator as one might expect.The following minimal script illustrates:
prints
One would reasonably expect
k in rowto beTrue.Tested on Python 3.10.0, Ubuntu 20.04, but not believed to be restricted to those.
Linked PRs
sqlite.Row.__contains__method #100457