Thanks to visit codestin.com Credit goes to github.com
We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 80d84c8 commit 72e731cCopy full SHA for 72e731c
4 files changed
Doc/library/sqlite3.rst
@@ -649,6 +649,9 @@ Row Objects
649
This method returns a list of column names. Immediately after a query,
650
it is the first member of each tuple in :attr:`Cursor.description`.
651
652
+ .. versionchanged:: 3.5
653
+ Added support of slicing.
654
+
655
Let's assume we initialize a table as in the example given above::
656
657
conn = sqlite3.connect(":memory:")
Lib/sqlite3/test/factory.py
@@ -111,6 +111,24 @@ def CheckSqliteRowIndex(self):
111
with self.assertRaises(IndexError):
112
row[2**1000]
113
114
+ def CheckSqliteRowSlice(self):
115
+ # A sqlite.Row can be sliced like a list.
116
+ self.con.row_factory = sqlite.Row
117
+ row = self.con.execute("select 1, 2, 3, 4").fetchone()
118
+ self.assertEqual(row[0:0], ())
119
+ self.assertEqual(row[0:1], (1,))
120
+ self.assertEqual(row[1:3], (2, 3))
121
+ self.assertEqual(row[3:1], ())
122
+ # Explicit bounds are optional.
123
+ self.assertEqual(row[1:], (2, 3, 4))
124
+ self.assertEqual(row[:3], (1, 2, 3))
125
+ # Slices can use negative indices.
126
+ self.assertEqual(row[-2:-1], (3,))
127
+ self.assertEqual(row[-2:], (3, 4))
128
+ # Slicing supports steps.
129
+ self.assertEqual(row[0:4:2], (1, 3))
130
+ self.assertEqual(row[3:0:-2], (4, 2))
131
132
def CheckSqliteRowIter(self):
133
"""Checks if the row object is iterable"""
134
self.con.row_factory = sqlite.Row
Misc/NEWS
@@ -13,6 +13,8 @@ Core and Builtins
13
Library
14
-------
15
16
+- Issue #13583: sqlite3.Row now supports slice indexing.
17
18
- Issue #18473: Fixed 2to3 and 3to2 compatible pickle mappings. Fixed
19
ambigious reverse mappings. Added many new mappings. Import mapping is no
20
longer applied to modules already mapped with full name mapping.
Modules/_sqlite/row.c
@@ -142,8 +142,7 @@ PyObject* pysqlite_row_subscript(pysqlite_Row* self, PyObject* idx)
142
return NULL;
143
}
144
else if (PySlice_Check(idx)) {
145
- PyErr_SetString(PyExc_ValueError, "slices not implemented, yet");
146
- return NULL;
+ return PyObject_GetItem(self->data, idx);
147
148
else {
149
PyErr_SetString(PyExc_IndexError, "Index must be int or string");
0 commit comments