Skip to content

Commit c7ac8b6

Browse files
[3.11] gh-94635: Add Reference, How-to, and Explanation headings to sqlite3 docs (GH-94636) (#95482)
Co-authored-by: CAM Gerlach <CAM.Gerlach@Gerlach.CAM>. (cherry picked from commit 6c439b9) Co-authored-by: Erlend Egeberg Aasland <erlend.aasland@protonmail.com> * Fix refs
1 parent 3ca0016 commit c7ac8b6

File tree

1 file changed

+124
-100
lines changed

1 file changed

+124
-100
lines changed

Doc/library/sqlite3.rst

+124-100
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,15 @@ The sqlite3 module was written by Gerhard Häring. It provides an SQL interface
2525
compliant with the DB-API 2.0 specification described by :pep:`249`, and
2626
requires SQLite 3.7.15 or newer.
2727

28+
This document includes four main sections:
29+
30+
* :ref:`sqlite3-tutorial` teaches how to use the sqlite3 module.
31+
* :ref:`sqlite3-reference` describes the classes and functions this module
32+
defines.
33+
* :ref:`sqlite3-howtos` details how to handle specific tasks.
34+
* :ref:`sqlite3-explanation` provides in-depth background on
35+
transaction control.
36+
2837

2938
.. _sqlite3-tutorial:
3039

@@ -136,10 +145,15 @@ both styles:
136145
PEP written by Marc-André Lemburg.
137146

138147

148+
.. _sqlite3-reference:
149+
150+
Reference
151+
---------
152+
139153
.. _sqlite3-module-contents:
140154

141155
Module functions and constants
142-
------------------------------
156+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
143157

144158

145159
.. data:: apilevel
@@ -411,8 +425,8 @@ Module functions and constants
411425

412426
.. _sqlite3-connection-objects:
413427

414-
Connection Objects
415-
------------------
428+
Connection objects
429+
^^^^^^^^^^^^^^^^^^
416430

417431
.. class:: Connection
418432

@@ -972,8 +986,8 @@ Connection Objects
972986

973987
.. _sqlite3-cursor-objects:
974988

975-
Cursor Objects
976-
--------------
989+
Cursor objects
990+
^^^^^^^^^^^^^^
977991

978992
A ``Cursor`` object represents a `database cursor`_
979993
which is used to execute SQL statements,
@@ -1149,8 +1163,8 @@ Cursor Objects
11491163

11501164
.. _sqlite3-row-objects:
11511165

1152-
Row Objects
1153-
-----------
1166+
Row objects
1167+
^^^^^^^^^^^
11541168

11551169
.. class:: Row
11561170

@@ -1212,8 +1226,10 @@ Now we plug :class:`Row` in::
12121226
35.14
12131227

12141228

1215-
Blob Objects
1216-
------------
1229+
.. _sqlite3-blob-objects:
1230+
1231+
Blob objects
1232+
^^^^^^^^^^^^
12171233

12181234
.. versionadded:: 3.11
12191235

@@ -1264,8 +1280,8 @@ Blob Objects
12641280
end).
12651281

12661282

1267-
PrepareProtocol Objects
1268-
-----------------------
1283+
PrepareProtocol objects
1284+
^^^^^^^^^^^^^^^^^^^^^^^
12691285

12701286
.. class:: PrepareProtocol
12711287

@@ -1277,7 +1293,7 @@ PrepareProtocol Objects
12771293
.. _sqlite3-exceptions:
12781294

12791295
Exceptions
1280-
----------
1296+
^^^^^^^^^^
12811297

12821298
The exception hierarchy is defined by the DB-API 2.0 (:pep:`249`).
12831299

@@ -1364,16 +1380,10 @@ The exception hierarchy is defined by the DB-API 2.0 (:pep:`249`).
13641380
``NotSupportedError`` is a subclass of :exc:`DatabaseError`.
13651381

13661382

1367-
.. _sqlite3-blob-objects:
1368-
13691383
.. _sqlite3-types:
13701384

13711385
SQLite and Python types
1372-
-----------------------
1373-
1374-
1375-
Introduction
1376-
^^^^^^^^^^^^
1386+
^^^^^^^^^^^^^^^^^^^^^^^
13771387

13781388
SQLite natively supports the following types: ``NULL``, ``INTEGER``,
13791389
``REAL``, ``TEXT``, ``BLOB``.
@@ -1413,10 +1423,18 @@ This is how SQLite types are converted to Python types by default:
14131423
+-------------+----------------------------------------------+
14141424

14151425
The type system of the :mod:`sqlite3` module is extensible in two ways: you can
1416-
store additional Python types in an SQLite database via object adaptation, and
1417-
you can let the :mod:`sqlite3` module convert SQLite types to different Python
1418-
types via converters.
1426+
store additional Python types in an SQLite database via
1427+
:ref:`object adapters <sqlite3-adapters>`,
1428+
and you can let the ``sqlite3`` module convert SQLite types to
1429+
Python types via :ref:`converters <sqlite3-converters>`.
1430+
14191431

1432+
.. _sqlite3-howtos:
1433+
1434+
How-to guides
1435+
-------------
1436+
1437+
.. _sqlite3-adapters:
14201438

14211439
Using adapters to store custom Python types in SQLite databases
14221440
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -1460,6 +1478,8 @@ This function can then be registered using :func:`register_adapter`.
14601478
.. literalinclude:: ../includes/sqlite3/adapter_point_2.py
14611479

14621480

1481+
.. _sqlite3-converters:
1482+
14631483
Converting SQLite values to custom Python types
14641484
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
14651485

@@ -1532,7 +1552,7 @@ timestamp converter.
15321552

15331553
.. _sqlite3-adapter-converter-recipes:
15341554

1535-
Adapter and Converter Recipes
1555+
Adapter and converter recipes
15361556
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
15371557

15381558
This section shows recipes for common adapters and converters.
@@ -1575,83 +1595,6 @@ This section shows recipes for common adapters and converters.
15751595
sqlite3.register_converter("timestamp", convert_timestamp)
15761596
15771597
1578-
.. _sqlite3-controlling-transactions:
1579-
1580-
Controlling Transactions
1581-
------------------------
1582-
1583-
The ``sqlite3`` module does not adhere to the transaction handling recommended
1584-
by :pep:`249`.
1585-
1586-
If the connection attribute :attr:`~Connection.isolation_level`
1587-
is not :const:`None`,
1588-
new transactions are implicitly opened before
1589-
:meth:`~Cursor.execute` and :meth:`~Cursor.executemany` executes
1590-
``INSERT``, ``UPDATE``, ``DELETE``, or ``REPLACE`` statements.
1591-
Use the :meth:`~Connection.commit` and :meth:`~Connection.rollback` methods
1592-
to respectively commit and roll back pending transactions.
1593-
You can choose the underlying `SQLite transaction behaviour`_ —
1594-
that is, whether and what type of ``BEGIN`` statements ``sqlite3``
1595-
implicitly executes –
1596-
via the :attr:`~Connection.isolation_level` attribute.
1597-
1598-
If :attr:`~Connection.isolation_level` is set to :const:`None`,
1599-
no transactions are implicitly opened at all.
1600-
This leaves the underlying SQLite library in `autocommit mode`_,
1601-
but also allows the user to perform their own transaction handling
1602-
using explicit SQL statements.
1603-
The underlying SQLite library autocommit mode can be queried using the
1604-
:attr:`~Connection.in_transaction` attribute.
1605-
1606-
The :meth:`~Cursor.executescript` method implicitly commits
1607-
any pending transaction before execution of the given SQL script,
1608-
regardless of the value of :attr:`~Connection.isolation_level`.
1609-
1610-
.. versionchanged:: 3.6
1611-
:mod:`sqlite3` used to implicitly commit an open transaction before DDL
1612-
statements. This is no longer the case.
1613-
1614-
.. _autocommit mode:
1615-
https://www.sqlite.org/lang_transaction.html#implicit_versus_explicit_transactions
1616-
1617-
.. _SQLite transaction behaviour:
1618-
https://www.sqlite.org/lang_transaction.html#deferred_immediate_and_exclusive_transactions
1619-
1620-
1621-
.. _sqlite3-uri-tricks:
1622-
1623-
SQLite URI tricks
1624-
-----------------
1625-
1626-
Some useful URI tricks include:
1627-
1628-
* Open a database in read-only mode::
1629-
1630-
con = sqlite3.connect("file:template.db?mode=ro", uri=True)
1631-
1632-
* Do not implicitly create a new database file if it does not already exist;
1633-
will raise :exc:`~sqlite3.OperationalError` if unable to create a new file::
1634-
1635-
con = sqlite3.connect("file:nosuchdb.db?mode=rw", uri=True)
1636-
1637-
* Create a shared named in-memory database::
1638-
1639-
con1 = sqlite3.connect("file:mem1?mode=memory&cache=shared", uri=True)
1640-
con2 = sqlite3.connect("file:mem1?mode=memory&cache=shared", uri=True)
1641-
con1.execute("create table t(t)")
1642-
con1.execute("insert into t values(28)")
1643-
con1.commit()
1644-
rows = con2.execute("select * from t").fetchall()
1645-
1646-
More information about this feature, including a list of parameters,
1647-
can be found in the `SQLite URI documentation`_.
1648-
1649-
.. _SQLite URI documentation: https://www.sqlite.org/uri.html
1650-
1651-
Using :mod:`sqlite3` efficiently
1652-
--------------------------------
1653-
1654-
16551598
.. _sqlite3-connection-shortcuts:
16561599

16571600
Using connection shortcut methods
@@ -1669,6 +1612,8 @@ directly using only a single call on the :class:`Connection` object.
16691612
.. literalinclude:: ../includes/sqlite3/shortcut_methods.py
16701613

16711614

1615+
.. _sqlite3-columns-by-name:
1616+
16721617
Accessing columns by name instead of by index
16731618
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
16741619

@@ -1704,3 +1649,82 @@ the context manager is a no-op.
17041649
nor closes the connection.
17051650

17061651
.. literalinclude:: ../includes/sqlite3/ctx_manager.py
1652+
1653+
1654+
.. _sqlite3-uri-tricks:
1655+
1656+
Working with SQLite URIs
1657+
^^^^^^^^^^^^^^^^^^^^^^^^
1658+
1659+
Some useful URI tricks include:
1660+
1661+
* Open a database in read-only mode::
1662+
1663+
con = sqlite3.connect("file:template.db?mode=ro", uri=True)
1664+
1665+
* Do not implicitly create a new database file if it does not already exist;
1666+
will raise :exc:`~sqlite3.OperationalError` if unable to create a new file::
1667+
1668+
con = sqlite3.connect("file:nosuchdb.db?mode=rw", uri=True)
1669+
1670+
* Create a shared named in-memory database::
1671+
1672+
con1 = sqlite3.connect("file:mem1?mode=memory&cache=shared", uri=True)
1673+
con2 = sqlite3.connect("file:mem1?mode=memory&cache=shared", uri=True)
1674+
con1.execute("create table t(t)")
1675+
con1.execute("insert into t values(28)")
1676+
con1.commit()
1677+
rows = con2.execute("select * from t").fetchall()
1678+
1679+
More information about this feature, including a list of parameters,
1680+
can be found in the `SQLite URI documentation`_.
1681+
1682+
.. _SQLite URI documentation: https://www.sqlite.org/uri.html
1683+
1684+
1685+
.. _sqlite3-explanation:
1686+
1687+
Explanation
1688+
-----------
1689+
1690+
.. _sqlite3-controlling-transactions:
1691+
1692+
Transaction control
1693+
^^^^^^^^^^^^^^^^^^^
1694+
1695+
The ``sqlite3`` module does not adhere to the transaction handling recommended
1696+
by :pep:`249`.
1697+
1698+
If the connection attribute :attr:`~Connection.isolation_level`
1699+
is not :const:`None`,
1700+
new transactions are implicitly opened before
1701+
:meth:`~Cursor.execute` and :meth:`~Cursor.executemany` executes
1702+
``INSERT``, ``UPDATE``, ``DELETE``, or ``REPLACE`` statements.
1703+
Use the :meth:`~Connection.commit` and :meth:`~Connection.rollback` methods
1704+
to respectively commit and roll back pending transactions.
1705+
You can choose the underlying `SQLite transaction behaviour`_ —
1706+
that is, whether and what type of ``BEGIN`` statements ``sqlite3``
1707+
implicitly executes –
1708+
via the :attr:`~Connection.isolation_level` attribute.
1709+
1710+
If :attr:`~Connection.isolation_level` is set to :const:`None`,
1711+
no transactions are implicitly opened at all.
1712+
This leaves the underlying SQLite library in `autocommit mode`_,
1713+
but also allows the user to perform their own transaction handling
1714+
using explicit SQL statements.
1715+
The underlying SQLite library autocommit mode can be queried using the
1716+
:attr:`~Connection.in_transaction` attribute.
1717+
1718+
The :meth:`~Cursor.executescript` method implicitly commits
1719+
any pending transaction before execution of the given SQL script,
1720+
regardless of the value of :attr:`~Connection.isolation_level`.
1721+
1722+
.. versionchanged:: 3.6
1723+
:mod:`sqlite3` used to implicitly commit an open transaction before DDL
1724+
statements. This is no longer the case.
1725+
1726+
.. _autocommit mode:
1727+
https://www.sqlite.org/lang_transaction.html#implicit_versus_explicit_transactions
1728+
1729+
.. _SQLite transaction behaviour:
1730+
https://www.sqlite.org/lang_transaction.html#deferred_immediate_and_exclusive_transactions

0 commit comments

Comments
 (0)