Skip to content

sqlite3.Row behaves like a dictionary but doesn't handle "in" as one might expect #100450

Closed as not planned
@vsajip

Description

@vsajip

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    Status

    Discarded

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions