Skip to content

Commit 94f0848

Browse files
CAM-Gerlacherlend-aasland
authored andcommitted
[3.11] pythongh-94635: Frame sqlite3 how-to headings as such & move default adapters to reference (pythonGH-96136)
Co-authored-by: Erlend E. Aasland <[email protected]> Co-authored-by: Ezio Melotti <[email protected]>. (cherry picked from commit 6bda5b8) Co-authored-by: C.A.M. Gerlach <[email protected]>
1 parent c01fc9d commit 94f0848

File tree

1 file changed

+45
-43
lines changed

1 file changed

+45
-43
lines changed

Doc/library/sqlite3.rst

Lines changed: 45 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1530,6 +1530,38 @@ and you can let the :mod:`!sqlite3` module convert SQLite types to
15301530
Python types via :ref:`converters <sqlite3-converters>`.
15311531

15321532

1533+
.. _sqlite3-default-converters:
1534+
1535+
Default adapters and converters
1536+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1537+
1538+
There are default adapters for the date and datetime types in the datetime
1539+
module. They will be sent as ISO dates/ISO timestamps to SQLite.
1540+
1541+
The default converters are registered under the name "date" for
1542+
:class:`datetime.date` and under the name "timestamp" for
1543+
:class:`datetime.datetime`.
1544+
1545+
This way, you can use date/timestamps from Python without any additional
1546+
fiddling in most cases. The format of the adapters is also compatible with the
1547+
experimental SQLite date/time functions.
1548+
1549+
The following example demonstrates this.
1550+
1551+
.. literalinclude:: ../includes/sqlite3/pysqlite_datetime.py
1552+
1553+
If a timestamp stored in SQLite has a fractional part longer than 6
1554+
numbers, its value will be truncated to microsecond precision by the
1555+
timestamp converter.
1556+
1557+
.. note::
1558+
1559+
The default "timestamp" converter ignores UTC offsets in the database and
1560+
always returns a naive :class:`datetime.datetime` object. To preserve UTC
1561+
offsets in timestamps, either leave converters disabled, or register an
1562+
offset-aware converter with :func:`register_converter`.
1563+
1564+
15331565
.. _sqlite3-howtos:
15341566

15351567
How-to guides
@@ -1567,8 +1599,8 @@ both styles:
15671599

15681600
.. _sqlite3-adapters:
15691601

1570-
Using adapters to store custom Python types in SQLite databases
1571-
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1602+
How to adapt custom Python types to SQLite values
1603+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
15721604

15731605
SQLite supports only a limited set of data types natively.
15741606
To store custom Python types in SQLite databases, *adapt* them to one of the
@@ -1585,8 +1617,8 @@ registering custom adapter functions.
15851617

15861618
.. _sqlite3-conform:
15871619

1588-
Letting your object adapt itself
1589-
""""""""""""""""""""""""""""""""
1620+
How to write adaptable objects
1621+
""""""""""""""""""""""""""""""
15901622

15911623
Suppose we have a :class:`!Point` class that represents a pair of coordinates,
15921624
``x`` and ``y``, in a Cartesian coordinate system.
@@ -1599,8 +1631,8 @@ The object passed to *protocol* will be of type :class:`PrepareProtocol`.
15991631
.. literalinclude:: ../includes/sqlite3/adapter_point_1.py
16001632

16011633

1602-
Registering an adapter callable
1603-
"""""""""""""""""""""""""""""""
1634+
How to register adapter callables
1635+
"""""""""""""""""""""""""""""""""
16041636

16051637
The other possibility is to create a function that converts the Python object
16061638
to an SQLite-compatible type.
@@ -1611,8 +1643,8 @@ This function can then be registered using :func:`register_adapter`.
16111643

16121644
.. _sqlite3-converters:
16131645

1614-
Converting SQLite values to custom Python types
1615-
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1646+
How to convert SQLite values to custom Python types
1647+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
16161648

16171649
Writing an adapter lets you convert *from* custom Python types *to* SQLite
16181650
values.
@@ -1651,36 +1683,6 @@ The following example illustrates the implicit and explicit approaches:
16511683
.. literalinclude:: ../includes/sqlite3/converter_point.py
16521684

16531685

1654-
Default adapters and converters
1655-
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1656-
1657-
There are default adapters for the date and datetime types in the datetime
1658-
module. They will be sent as ISO dates/ISO timestamps to SQLite.
1659-
1660-
The default converters are registered under the name "date" for
1661-
:class:`datetime.date` and under the name "timestamp" for
1662-
:class:`datetime.datetime`.
1663-
1664-
This way, you can use date/timestamps from Python without any additional
1665-
fiddling in most cases. The format of the adapters is also compatible with the
1666-
experimental SQLite date/time functions.
1667-
1668-
The following example demonstrates this.
1669-
1670-
.. literalinclude:: ../includes/sqlite3/pysqlite_datetime.py
1671-
1672-
If a timestamp stored in SQLite has a fractional part longer than 6
1673-
numbers, its value will be truncated to microsecond precision by the
1674-
timestamp converter.
1675-
1676-
.. note::
1677-
1678-
The default "timestamp" converter ignores UTC offsets in the database and
1679-
always returns a naive :class:`datetime.datetime` object. To preserve UTC
1680-
offsets in timestamps, either leave converters disabled, or register an
1681-
offset-aware converter with :func:`register_converter`.
1682-
1683-
16841686
.. _sqlite3-adapter-converter-recipes:
16851687

16861688
Adapter and converter recipes
@@ -1728,8 +1730,8 @@ This section shows recipes for common adapters and converters.
17281730
17291731
.. _sqlite3-connection-shortcuts:
17301732

1731-
Using connection shortcut methods
1732-
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1733+
How to use connection shortcut methods
1734+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
17331735

17341736
Using the :meth:`~Connection.execute`,
17351737
:meth:`~Connection.executemany`, and :meth:`~Connection.executescript`
@@ -1745,7 +1747,7 @@ directly using only a single call on the :class:`Connection` object.
17451747

17461748
.. _sqlite3-connection-context-manager:
17471749

1748-
Using the connection as a context manager
1750+
How to use the connection context manager
17491751
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
17501752

17511753
A :class:`Connection` object can be used as a context manager that
@@ -1770,8 +1772,8 @@ the context manager is a no-op.
17701772

17711773
.. _sqlite3-uri-tricks:
17721774

1773-
Working with SQLite URIs
1774-
^^^^^^^^^^^^^^^^^^^^^^^^
1775+
How to work with SQLite URIs
1776+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
17751777

17761778
Some useful URI tricks include:
17771779

0 commit comments

Comments
 (0)