Skip to content

Commit a836204

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 5a094f0 commit a836204

File tree

2 files changed

+18
-70
lines changed

2 files changed

+18
-70
lines changed

Doc/includes/sqlite3/rowclass.py

-14
This file was deleted.

Doc/library/sqlite3.rst

+18-56
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.
@@ -1017,17 +1016,21 @@ Cursor objects
10171016
>>> cur.connection == con
10181017
True
10191018

1019+
.. The sqlite3.Row example used to be a how-to. It has now been incorporated
1020+
into the Row reference. We keep the anchor here in order not to break
1021+
existing links.
1022+
1023+
.. _sqlite3-columns-by-name:
10201024
.. _sqlite3-row-objects:
10211025

10221026
Row objects
10231027
^^^^^^^^^^^
10241028

10251029
.. class:: Row
10261030

1027-
A :class:`Row` instance serves as a highly optimized
1031+
A :class:`!Row` instance serves as a highly optimized
10281032
:attr:`~Connection.row_factory` for :class:`Connection` objects.
1029-
It tries to mimic a :class:`tuple` in most of its features,
1030-
and supports iteration, :func:`repr`, equality testing, :func:`len`,
1033+
It supports iteration, equality testing, :func:`len`,
10311034
and :term:`mapping` access by column name and index.
10321035

10331036
Two row objects compare equal if have equal columns and equal members.
@@ -1041,45 +1044,18 @@ Row objects
10411044
.. versionchanged:: 3.5
10421045
Added support of slicing.
10431046

1044-
Let's assume we initialize a table as in the example given above::
1047+
Example::
10451048

1046-
con = sqlite3.connect(":memory:")
1047-
cur = con.cursor()
1048-
cur.execute('''create table stocks
1049-
(date text, trans text, symbol text,
1050-
qty real, price real)''')
1051-
cur.execute("""insert into stocks
1052-
values ('2006-01-05','BUY','RHAT',100,35.14)""")
1053-
con.commit()
1054-
cur.close()
1055-
1056-
Now we plug :class:`Row` in::
1057-
1058-
>>> con.row_factory = sqlite3.Row
1059-
>>> cur = con.cursor()
1060-
>>> cur.execute('select * from stocks')
1061-
<sqlite3.Cursor object at 0x7f4e7dd8fa80>
1062-
>>> r = cur.fetchone()
1063-
>>> type(r)
1064-
<class 'sqlite3.Row'>
1065-
>>> tuple(r)
1066-
('2006-01-05', 'BUY', 'RHAT', 100.0, 35.14)
1067-
>>> len(r)
1068-
5
1069-
>>> r[2]
1070-
'RHAT'
1071-
>>> r.keys()
1072-
['date', 'trans', 'symbol', 'qty', 'price']
1073-
>>> r['qty']
1074-
100.0
1075-
>>> for member in r:
1076-
... print(member)
1077-
...
1078-
2006-01-05
1079-
BUY
1080-
RHAT
1081-
100.0
1082-
35.14
1049+
>>> con = sqlite3.connect(":memory:")
1050+
>>> con.row_factory = sqlite3.Row
1051+
>>> res = con.execute("SELECT 'Earth' AS name, 6378 AS radius")
1052+
>>> row = res.fetchone()
1053+
>>> row.keys()
1054+
['name', 'radius']
1055+
>>> row[0], row["name"] # Access by index and name.
1056+
('Earth', 'Earth')
1057+
>>> row["RADIUS"] # Column names are case-insensitive.
1058+
6378
10831059

10841060

10851061
PrepareProtocol objects
@@ -1429,20 +1405,6 @@ directly using only a single call on the :class:`Connection` object.
14291405
.. literalinclude:: ../includes/sqlite3/shortcut_methods.py
14301406

14311407

1432-
.. _sqlite3-columns-by-name:
1433-
1434-
Accessing columns by name instead of by index
1435-
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1436-
1437-
One useful feature of the :mod:`!sqlite3` module is the built-in
1438-
:class:`sqlite3.Row` class designed to be used as a row factory.
1439-
1440-
Rows wrapped with this class can be accessed both by index (like tuples) and
1441-
case-insensitively by name:
1442-
1443-
.. literalinclude:: ../includes/sqlite3/rowclass.py
1444-
1445-
14461408
.. _sqlite3-connection-context-manager:
14471409

14481410
Using the connection as a context manager

0 commit comments

Comments
 (0)