mirror of
https://github.com/python/cpython.git
synced 2025-10-03 13:45:29 +00:00
gh-96121: Merge sqlite3.Row examples into sqlite3.Row class doc (GH-96122)
Co-authored-by: Ezio Melotti <ezio.melotti@gmail.com>
Co-authored-by: C.A.M. Gerlach <CAM.Gerlach@Gerlach.CAM>
(cherry picked from commit 18b1782192
)
Co-authored-by: Erlend E. Aasland <erlend.aasland@innova.no>
This commit is contained in:
parent
fedd25eb64
commit
98622fa00e
2 changed files with 18 additions and 70 deletions
|
@ -1,14 +0,0 @@
|
||||||
import sqlite3
|
|
||||||
|
|
||||||
con = sqlite3.connect(":memory:")
|
|
||||||
con.row_factory = sqlite3.Row
|
|
||||||
|
|
||||||
cur = con.cursor()
|
|
||||||
cur.execute("select 'John' as name, 42 as age")
|
|
||||||
for row in cur:
|
|
||||||
assert row[0] == row["name"]
|
|
||||||
assert row["name"] == row["nAmE"]
|
|
||||||
assert row[1] == row["age"]
|
|
||||||
assert row[1] == row["AgE"]
|
|
||||||
|
|
||||||
con.close()
|
|
|
@ -207,7 +207,6 @@ inserted data and retrieved values from it in multiple ways.
|
||||||
* :ref:`sqlite3-placeholders`
|
* :ref:`sqlite3-placeholders`
|
||||||
* :ref:`sqlite3-adapters`
|
* :ref:`sqlite3-adapters`
|
||||||
* :ref:`sqlite3-converters`
|
* :ref:`sqlite3-converters`
|
||||||
* :ref:`sqlite3-columns-by-name`
|
|
||||||
* :ref:`sqlite3-connection-context-manager`
|
* :ref:`sqlite3-connection-context-manager`
|
||||||
|
|
||||||
* :ref:`sqlite3-explanation` for in-depth background on transaction control.
|
* :ref:`sqlite3-explanation` for in-depth background on transaction control.
|
||||||
|
@ -1242,6 +1241,11 @@ Cursor objects
|
||||||
>>> cur.connection == con
|
>>> cur.connection == con
|
||||||
True
|
True
|
||||||
|
|
||||||
|
.. The sqlite3.Row example used to be a how-to. It has now been incorporated
|
||||||
|
into the Row reference. We keep the anchor here in order not to break
|
||||||
|
existing links.
|
||||||
|
|
||||||
|
.. _sqlite3-columns-by-name:
|
||||||
.. _sqlite3-row-objects:
|
.. _sqlite3-row-objects:
|
||||||
|
|
||||||
Row objects
|
Row objects
|
||||||
|
@ -1249,10 +1253,9 @@ Row objects
|
||||||
|
|
||||||
.. class:: Row
|
.. class:: Row
|
||||||
|
|
||||||
A :class:`Row` instance serves as a highly optimized
|
A :class:`!Row` instance serves as a highly optimized
|
||||||
:attr:`~Connection.row_factory` for :class:`Connection` objects.
|
:attr:`~Connection.row_factory` for :class:`Connection` objects.
|
||||||
It tries to mimic a :class:`tuple` in most of its features,
|
It supports iteration, equality testing, :func:`len`,
|
||||||
and supports iteration, :func:`repr`, equality testing, :func:`len`,
|
|
||||||
and :term:`mapping` access by column name and index.
|
and :term:`mapping` access by column name and index.
|
||||||
|
|
||||||
Two row objects compare equal if have equal columns and equal members.
|
Two row objects compare equal if have equal columns and equal members.
|
||||||
|
@ -1266,45 +1269,18 @@ Row objects
|
||||||
.. versionchanged:: 3.5
|
.. versionchanged:: 3.5
|
||||||
Added support of slicing.
|
Added support of slicing.
|
||||||
|
|
||||||
Let's assume we initialize a table as in the example given above::
|
Example::
|
||||||
|
|
||||||
con = sqlite3.connect(":memory:")
|
>>> con = sqlite3.connect(":memory:")
|
||||||
cur = con.cursor()
|
>>> con.row_factory = sqlite3.Row
|
||||||
cur.execute('''create table stocks
|
>>> res = con.execute("SELECT 'Earth' AS name, 6378 AS radius")
|
||||||
(date text, trans text, symbol text,
|
>>> row = res.fetchone()
|
||||||
qty real, price real)''')
|
>>> row.keys()
|
||||||
cur.execute("""insert into stocks
|
['name', 'radius']
|
||||||
values ('2006-01-05','BUY','RHAT',100,35.14)""")
|
>>> row[0], row["name"] # Access by index and name.
|
||||||
con.commit()
|
('Earth', 'Earth')
|
||||||
cur.close()
|
>>> row["RADIUS"] # Column names are case-insensitive.
|
||||||
|
6378
|
||||||
Now we plug :class:`Row` in::
|
|
||||||
|
|
||||||
>>> con.row_factory = sqlite3.Row
|
|
||||||
>>> cur = con.cursor()
|
|
||||||
>>> cur.execute('select * from stocks')
|
|
||||||
<sqlite3.Cursor object at 0x7f4e7dd8fa80>
|
|
||||||
>>> r = cur.fetchone()
|
|
||||||
>>> type(r)
|
|
||||||
<class 'sqlite3.Row'>
|
|
||||||
>>> tuple(r)
|
|
||||||
('2006-01-05', 'BUY', 'RHAT', 100.0, 35.14)
|
|
||||||
>>> len(r)
|
|
||||||
5
|
|
||||||
>>> r[2]
|
|
||||||
'RHAT'
|
|
||||||
>>> r.keys()
|
|
||||||
['date', 'trans', 'symbol', 'qty', 'price']
|
|
||||||
>>> r['qty']
|
|
||||||
100.0
|
|
||||||
>>> for member in r:
|
|
||||||
... print(member)
|
|
||||||
...
|
|
||||||
2006-01-05
|
|
||||||
BUY
|
|
||||||
RHAT
|
|
||||||
100.0
|
|
||||||
35.14
|
|
||||||
|
|
||||||
|
|
||||||
.. _sqlite3-blob-objects:
|
.. _sqlite3-blob-objects:
|
||||||
|
@ -1726,20 +1702,6 @@ directly using only a single call on the :class:`Connection` object.
|
||||||
.. literalinclude:: ../includes/sqlite3/shortcut_methods.py
|
.. literalinclude:: ../includes/sqlite3/shortcut_methods.py
|
||||||
|
|
||||||
|
|
||||||
.. _sqlite3-columns-by-name:
|
|
||||||
|
|
||||||
Accessing columns by name instead of by index
|
|
||||||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
|
||||||
|
|
||||||
One useful feature of the :mod:`!sqlite3` module is the built-in
|
|
||||||
:class:`sqlite3.Row` class designed to be used as a row factory.
|
|
||||||
|
|
||||||
Rows wrapped with this class can be accessed both by index (like tuples) and
|
|
||||||
case-insensitively by name:
|
|
||||||
|
|
||||||
.. literalinclude:: ../includes/sqlite3/rowclass.py
|
|
||||||
|
|
||||||
|
|
||||||
.. _sqlite3-connection-context-manager:
|
.. _sqlite3-connection-context-manager:
|
||||||
|
|
||||||
Using the connection as a context manager
|
Using the connection as a context manager
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue