summaryrefslogtreecommitdiffstats
path: root/Doc/library
diff options
context:
space:
mode:
authorErlend E. Aasland <erlend.aasland@protonmail.com>2022-08-24 07:52:16 (GMT)
committerGitHub <noreply@github.com>2022-08-24 07:52:16 (GMT)
commit203b598e5113f3662bfbaf3474dd62a258b8b6ce (patch)
tree55fe9d6d7f258f8aea2be100c9fd64bc145cc8e3 /Doc/library
parente3c4a5b8ede2f239c8a3de5e0f0a755b67f6c324 (diff)
downloadcpython-203b598e5113f3662bfbaf3474dd62a258b8b6ce.zip
cpython-203b598e5113f3662bfbaf3474dd62a258b8b6ce.tar.gz
cpython-203b598e5113f3662bfbaf3474dd62a258b8b6ce.tar.bz2
[3.10] gh-94635: Frame sqlite3 how-to headings as such & move default adapters to reference (GH-96136) (#96227)
Co-authored-by: Erlend E. Aasland <erlend.aasland@protonmail.com> Co-authored-by: Ezio Melotti <ezio.melotti@gmail.com>. (cherry picked from commit 6bda5b85b53443f3467865fbf85cbe72932e7cd6) Co-authored-by: C.A.M. Gerlach <CAM.Gerlach@Gerlach.CAM>
Diffstat (limited to 'Doc/library')
-rw-r--r--Doc/library/sqlite3.rst88
1 files changed, 45 insertions, 43 deletions
diff --git a/Doc/library/sqlite3.rst b/Doc/library/sqlite3.rst
index be9a22e..94d1e1b 100644
--- a/Doc/library/sqlite3.rst
+++ b/Doc/library/sqlite3.rst
@@ -1233,6 +1233,38 @@ and you can let the :mod:`!sqlite3` module convert SQLite types to
Python types via :ref:`converters <sqlite3-converters>`.
+.. _sqlite3-default-converters:
+
+Default adapters and converters
+^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+
+There are default adapters for the date and datetime types in the datetime
+module. They will be sent as ISO dates/ISO timestamps to SQLite.
+
+The default converters are registered under the name "date" for
+:class:`datetime.date` and under the name "timestamp" for
+:class:`datetime.datetime`.
+
+This way, you can use date/timestamps from Python without any additional
+fiddling in most cases. The format of the adapters is also compatible with the
+experimental SQLite date/time functions.
+
+The following example demonstrates this.
+
+.. literalinclude:: ../includes/sqlite3/pysqlite_datetime.py
+
+If a timestamp stored in SQLite has a fractional part longer than 6
+numbers, its value will be truncated to microsecond precision by the
+timestamp converter.
+
+.. note::
+
+ The default "timestamp" converter ignores UTC offsets in the database and
+ always returns a naive :class:`datetime.datetime` object. To preserve UTC
+ offsets in timestamps, either leave converters disabled, or register an
+ offset-aware converter with :func:`register_converter`.
+
+
.. _sqlite3-howtos:
How-to guides
@@ -1270,8 +1302,8 @@ both styles:
.. _sqlite3-adapters:
-Using adapters to store custom Python types in SQLite databases
-^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+How to adapt custom Python types to SQLite values
+^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
SQLite supports only a limited set of data types natively.
To store custom Python types in SQLite databases, *adapt* them to one of the
@@ -1288,8 +1320,8 @@ registering custom adapter functions.
.. _sqlite3-conform:
-Letting your object adapt itself
-""""""""""""""""""""""""""""""""
+How to write adaptable objects
+""""""""""""""""""""""""""""""
Suppose we have a :class:`!Point` class that represents a pair of coordinates,
``x`` and ``y``, in a Cartesian coordinate system.
@@ -1302,8 +1334,8 @@ The object passed to *protocol* will be of type :class:`PrepareProtocol`.
.. literalinclude:: ../includes/sqlite3/adapter_point_1.py
-Registering an adapter callable
-"""""""""""""""""""""""""""""""
+How to register adapter callables
+"""""""""""""""""""""""""""""""""
The other possibility is to create a function that converts the Python object
to an SQLite-compatible type.
@@ -1314,8 +1346,8 @@ This function can then be registered using :func:`register_adapter`.
.. _sqlite3-converters:
-Converting SQLite values to custom Python types
-^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+How to convert SQLite values to custom Python types
+^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Writing an adapter lets you convert *from* custom Python types *to* SQLite
values.
@@ -1354,36 +1386,6 @@ The following example illustrates the implicit and explicit approaches:
.. literalinclude:: ../includes/sqlite3/converter_point.py
-Default adapters and converters
-^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
-
-There are default adapters for the date and datetime types in the datetime
-module. They will be sent as ISO dates/ISO timestamps to SQLite.
-
-The default converters are registered under the name "date" for
-:class:`datetime.date` and under the name "timestamp" for
-:class:`datetime.datetime`.
-
-This way, you can use date/timestamps from Python without any additional
-fiddling in most cases. The format of the adapters is also compatible with the
-experimental SQLite date/time functions.
-
-The following example demonstrates this.
-
-.. literalinclude:: ../includes/sqlite3/pysqlite_datetime.py
-
-If a timestamp stored in SQLite has a fractional part longer than 6
-numbers, its value will be truncated to microsecond precision by the
-timestamp converter.
-
-.. note::
-
- The default "timestamp" converter ignores UTC offsets in the database and
- always returns a naive :class:`datetime.datetime` object. To preserve UTC
- offsets in timestamps, either leave converters disabled, or register an
- offset-aware converter with :func:`register_converter`.
-
-
.. _sqlite3-adapter-converter-recipes:
Adapter and converter recipes
@@ -1431,8 +1433,8 @@ This section shows recipes for common adapters and converters.
.. _sqlite3-connection-shortcuts:
-Using connection shortcut methods
-^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+How to use connection shortcut methods
+^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Using the :meth:`~Connection.execute`,
:meth:`~Connection.executemany`, and :meth:`~Connection.executescript`
@@ -1448,7 +1450,7 @@ directly using only a single call on the :class:`Connection` object.
.. _sqlite3-connection-context-manager:
-Using the connection as a context manager
+How to use the connection context manager
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
A :class:`Connection` object can be used as a context manager that
@@ -1473,8 +1475,8 @@ the context manager is a no-op.
.. _sqlite3-uri-tricks:
-Working with SQLite URIs
-^^^^^^^^^^^^^^^^^^^^^^^^
+How to work with SQLite URIs
+^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Some useful URI tricks include: