Skip to content

Commit 18b1782

Browse files
Erlend E. Aaslandezio-melottiCAM-Gerlach
authored
pythongh-96121: Merge sqlite3.Row examples into sqlite3.Row class doc (python#96122)
Co-authored-by: Ezio Melotti <ezio.melotti@gmail.com> Co-authored-by: C.A.M. Gerlach <CAM.Gerlach@Gerlach.CAM>
1 parent 53e6a9a commit 18b1782

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.
@@ -1255,17 +1254,21 @@ Cursor objects
12551254
>>> cur.connection == con
12561255
True
12571256

1257+
.. The sqlite3.Row example used to be a how-to. It has now been incorporated
1258+
into the Row reference. We keep the anchor here in order not to break
1259+
existing links.
1260+
1261+
.. _sqlite3-columns-by-name:
12581262
.. _sqlite3-row-objects:
12591263

12601264
Row objects
12611265
^^^^^^^^^^^
12621266

12631267
.. class:: Row
12641268

1265-
A :class:`Row` instance serves as a highly optimized
1269+
A :class:`!Row` instance serves as a highly optimized
12661270
:attr:`~Connection.row_factory` for :class:`Connection` objects.
1267-
It tries to mimic a :class:`tuple` in most of its features,
1268-
and supports iteration, :func:`repr`, equality testing, :func:`len`,
1271+
It supports iteration, equality testing, :func:`len`,
12691272
and :term:`mapping` access by column name and index.
12701273

12711274
Two row objects compare equal if have equal columns and equal members.
@@ -1279,45 +1282,18 @@ Row objects
12791282
.. versionchanged:: 3.5
12801283
Added support of slicing.
12811284

1282-
Let's assume we initialize a table as in the example given above::
1285+
Example::
12831286

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

13221298

13231299
.. _sqlite3-blob-objects:
@@ -1766,20 +1742,6 @@ directly using only a single call on the :class:`Connection` object.
17661742
.. literalinclude:: ../includes/sqlite3/shortcut_methods.py
17671743

17681744

1769-
.. _sqlite3-columns-by-name:
1770-
1771-
Accessing columns by name instead of by index
1772-
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1773-
1774-
One useful feature of the :mod:`!sqlite3` module is the built-in
1775-
:class:`sqlite3.Row` class designed to be used as a row factory.
1776-
1777-
Rows wrapped with this class can be accessed both by index (like tuples) and
1778-
case-insensitively by name:
1779-
1780-
.. literalinclude:: ../includes/sqlite3/rowclass.py
1781-
1782-
17831745
.. _sqlite3-connection-context-manager:
17841746

17851747
Using the connection as a context manager

0 commit comments

Comments
 (0)