Skip to content

Commit 6c439b9

Browse files
gh-94635: Add Reference, How-to, and Concepts headings to sqlite3 docs (#94636)
Co-authored-by: CAM Gerlach <[email protected]>
1 parent 8281462 commit 6c439b9

File tree

1 file changed

+120
-98
lines changed

1 file changed

+120
-98
lines changed

Doc/library/sqlite3.rst

Lines changed: 120 additions & 98 deletions
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
@@ -421,8 +435,8 @@ Module functions and constants
421435

422436
.. _sqlite3-connection-objects:
423437

424-
Connection Objects
425-
------------------
438+
Connection objects
439+
^^^^^^^^^^^^^^^^^^
426440

427441
.. class:: Connection
428442

@@ -982,8 +996,8 @@ Connection Objects
982996

983997
.. _sqlite3-cursor-objects:
984998

985-
Cursor Objects
986-
--------------
999+
Cursor objects
1000+
^^^^^^^^^^^^^^
9871001

9881002
A ``Cursor`` object represents a `database cursor`_
9891003
which is used to execute SQL statements,
@@ -1159,8 +1173,8 @@ Cursor Objects
11591173

11601174
.. _sqlite3-row-objects:
11611175

1162-
Row Objects
1163-
-----------
1176+
Row objects
1177+
^^^^^^^^^^^
11641178

11651179
.. class:: Row
11661180

@@ -1224,8 +1238,8 @@ Now we plug :class:`Row` in::
12241238

12251239
.. _sqlite3-blob-objects:
12261240

1227-
Blob Objects
1228-
------------
1241+
Blob objects
1242+
^^^^^^^^^^^^
12291243

12301244
.. versionadded:: 3.11
12311245

@@ -1276,8 +1290,8 @@ Blob Objects
12761290
end).
12771291

12781292

1279-
PrepareProtocol Objects
1280-
-----------------------
1293+
PrepareProtocol objects
1294+
^^^^^^^^^^^^^^^^^^^^^^^
12811295

12821296
.. class:: PrepareProtocol
12831297

@@ -1289,7 +1303,7 @@ PrepareProtocol Objects
12891303
.. _sqlite3-exceptions:
12901304

12911305
Exceptions
1292-
----------
1306+
^^^^^^^^^^
12931307

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

@@ -1379,11 +1393,7 @@ The exception hierarchy is defined by the DB-API 2.0 (:pep:`249`).
13791393
.. _sqlite3-types:
13801394

13811395
SQLite and Python types
1382-
-----------------------
1383-
1384-
1385-
Introduction
1386-
^^^^^^^^^^^^
1396+
^^^^^^^^^^^^^^^^^^^^^^^
13871397

13881398
SQLite natively supports the following types: ``NULL``, ``INTEGER``,
13891399
``REAL``, ``TEXT``, ``BLOB``.
@@ -1423,10 +1433,18 @@ This is how SQLite types are converted to Python types by default:
14231433
+-------------+----------------------------------------------+
14241434

14251435
The type system of the :mod:`sqlite3` module is extensible in two ways: you can
1426-
store additional Python types in an SQLite database via object adaptation, and
1427-
you can let the :mod:`sqlite3` module convert SQLite types to different Python
1428-
types via converters.
1436+
store additional Python types in an SQLite database via
1437+
:ref:`object adapters <sqlite3-adapters>`,
1438+
and you can let the ``sqlite3`` module convert SQLite types to
1439+
Python types via :ref:`converters <sqlite3-converters>`.
1440+
1441+
1442+
.. _sqlite3-howtos:
14291443

1444+
How-to guides
1445+
-------------
1446+
1447+
.. _sqlite3-adapters:
14301448

14311449
Using adapters to store custom Python types in SQLite databases
14321450
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -1549,7 +1567,7 @@ The deprecated default adapters and converters consist of:
15491567

15501568
.. _sqlite3-adapter-converter-recipes:
15511569

1552-
Adapter and Converter Recipes
1570+
Adapter and converter recipes
15531571
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
15541572

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

16741615
Using connection shortcut methods
@@ -1686,6 +1627,8 @@ directly using only a single call on the :class:`Connection` object.
16861627
.. literalinclude:: ../includes/sqlite3/shortcut_methods.py
16871628

16881629

1630+
.. _sqlite3-columns-by-name:
1631+
16891632
Accessing columns by name instead of by index
16901633
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
16911634

@@ -1721,3 +1664,82 @@ the context manager is a no-op.
17211664
nor closes the connection.
17221665

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

0 commit comments

Comments
 (0)