Skip to content

Commit 98622fa

Browse files
miss-islingtonErlend E. Aasland
and
Erlend E. Aasland
authored
gh-96121: Merge sqlite3.Row examples into sqlite3.Row class doc (GH-96122)
Co-authored-by: Ezio Melotti <[email protected]> Co-authored-by: C.A.M. Gerlach <[email protected]> (cherry picked from commit 18b1782) Co-authored-by: Erlend E. Aasland <[email protected]>
1 parent fedd25e commit 98622fa

File tree

2 files changed

+18
-70
lines changed

2 files changed

+18
-70
lines changed

Doc/includes/sqlite3/rowclass.py

Lines changed: 0 additions & 14 deletions
This file was deleted.

Doc/library/sqlite3.rst

Lines changed: 18 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,6 @@ inserted data and retrieved values from it in multiple ways.
207207
* :ref:`sqlite3-placeholders`
208208
* :ref:`sqlite3-adapters`
209209
* :ref:`sqlite3-converters`
210-
* :ref:`sqlite3-columns-by-name`
211210
* :ref:`sqlite3-connection-context-manager`
212211

213212
* :ref:`sqlite3-explanation` for in-depth background on transaction control.
@@ -1242,17 +1241,21 @@ Cursor objects
12421241
>>> cur.connection == con
12431242
True
12441243

1244+
.. The sqlite3.Row example used to be a how-to. It has now been incorporated
1245+
into the Row reference. We keep the anchor here in order not to break
1246+
existing links.
1247+
1248+
.. _sqlite3-columns-by-name:
12451249
.. _sqlite3-row-objects:
12461250

12471251
Row objects
12481252
^^^^^^^^^^^
12491253

12501254
.. class:: Row
12511255

1252-
A :class:`Row` instance serves as a highly optimized
1256+
A :class:`!Row` instance serves as a highly optimized
12531257
:attr:`~Connection.row_factory` for :class:`Connection` objects.
1254-
It tries to mimic a :class:`tuple` in most of its features,
1255-
and supports iteration, :func:`repr`, equality testing, :func:`len`,
1258+
It supports iteration, equality testing, :func:`len`,
12561259
and :term:`mapping` access by column name and index.
12571260

12581261
Two row objects compare equal if have equal columns and equal members.
@@ -1266,45 +1269,18 @@ Row objects
12661269
.. versionchanged:: 3.5
12671270
Added support of slicing.
12681271

1269-
Let's assume we initialize a table as in the example given above::
1272+
Example::
12701273

1271-
con = sqlite3.connect(":memory:")
1272-
cur = con.cursor()
1273-
cur.execute('''create table stocks
1274-
(date text, trans text, symbol text,
1275-
qty real, price real)''')
1276-
cur.execute("""insert into stocks
1277-
values ('2006-01-05','BUY','RHAT',100,35.14)""")
1278-
con.commit()
1279-
cur.close()
1280-
1281-
Now we plug :class:`Row` in::
1282-
1283-
>>> con.row_factory = sqlite3.Row
1284-
>>> cur = con.cursor()
1285-
>>> cur.execute('select * from stocks')
1286-
<sqlite3.Cursor object at 0x7f4e7dd8fa80>
1287-
>>> r = cur.fetchone()
1288-
>>> type(r)
1289-
<class 'sqlite3.Row'>
1290-
>>> tuple(r)
1291-
('2006-01-05', 'BUY', 'RHAT', 100.0, 35.14)
1292-
>>> len(r)
1293-
5
1294-
>>> r[2]
1295-
'RHAT'
1296-
>>> r.keys()
1297-
['date', 'trans', 'symbol', 'qty', 'price']
1298-
>>> r['qty']
1299-
100.0
1300-
>>> for member in r:
1301-
... print(member)
1302-
...
1303-
2006-01-05
1304-
BUY
1305-
RHAT
1306-
100.0
1307-
35.14
1274+
>>> con = sqlite3.connect(":memory:")
1275+
>>> con.row_factory = sqlite3.Row
1276+
>>> res = con.execute("SELECT 'Earth' AS name, 6378 AS radius")
1277+
>>> row = res.fetchone()
1278+
>>> row.keys()
1279+
['name', 'radius']
1280+
>>> row[0], row["name"] # Access by index and name.
1281+
('Earth', 'Earth')
1282+
>>> row["RADIUS"] # Column names are case-insensitive.
1283+
6378
13081284

13091285

13101286
.. _sqlite3-blob-objects:
@@ -1726,20 +1702,6 @@ directly using only a single call on the :class:`Connection` object.
17261702
.. literalinclude:: ../includes/sqlite3/shortcut_methods.py
17271703

17281704

1729-
.. _sqlite3-columns-by-name:
1730-
1731-
Accessing columns by name instead of by index
1732-
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1733-
1734-
One useful feature of the :mod:`!sqlite3` module is the built-in
1735-
:class:`sqlite3.Row` class designed to be used as a row factory.
1736-
1737-
Rows wrapped with this class can be accessed both by index (like tuples) and
1738-
case-insensitively by name:
1739-
1740-
.. literalinclude:: ../includes/sqlite3/rowclass.py
1741-
1742-
17431705
.. _sqlite3-connection-context-manager:
17441706

17451707
Using the connection as a context manager

0 commit comments

Comments
 (0)