Skip to content
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Doc/library/sqlite3.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2392,6 +2392,10 @@ Queries now return :class:`!Row` objects:
'Earth'
>>> row["RADIUS"] # Column names are case-insensitive.
6378
>>> 'name' in row
True
>>> 'missing_field' in row
False

You can create a custom :attr:`~Cursor.row_factory`
that returns each row as a :class:`dict`, with column names mapped to values:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add ``sqlite.Row.__contains__`` method.
20 changes: 20 additions & 0 deletions Modules/_sqlite/row.c
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,25 @@ PyObject* pysqlite_row_item(pysqlite_Row* self, Py_ssize_t idx)
return Py_XNewRef(item);
}

static int
pysqlite_row_contains(pysqlite_Row* self, PyObject *arg)
{
Py_ssize_t nitems, i;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use intermingled declarations :)

int cmp = 0;

nitems = PyTuple_Size(self->description);

for (i = 0; cmp == 0 && i < nitems; i++) {
cmp = PyObject_RichCompareBool(
arg,
PyTuple_GET_ITEM(PyTuple_GET_ITEM(self->description, i), 0),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a lot happening on this line. I'd prefer it if you extracted these items before calling PyObject_RichCompareBool, for improved readability.

Py_EQ
);
}

return cmp;
}

static int
equal_ignore_case(PyObject *left, PyObject *right)
{
Expand Down Expand Up @@ -249,6 +268,7 @@ static PyType_Slot row_slots[] = {
{Py_mp_subscript, pysqlite_row_subscript},
{Py_sq_length, pysqlite_row_length},
{Py_sq_item, pysqlite_row_item},
{Py_sq_contains, pysqlite_row_contains},
{Py_tp_new, pysqlite_row_new},
{Py_tp_traverse, row_traverse},
{Py_tp_clear, row_clear},
Expand Down