Skip to content

Commit 203b598

Browse files
[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 <[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 e3c4a5b commit 203b598

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
@@ -1233,6 +1233,38 @@ and you can let the :mod:`!sqlite3` module convert SQLite types to
12331233
Python types via :ref:`converters <sqlite3-converters>`.
12341234

12351235

1236+
.. _sqlite3-default-converters:
1237+
1238+
Default adapters and converters
1239+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1240+
1241+
There are default adapters for the date and datetime types in the datetime
1242+
module. They will be sent as ISO dates/ISO timestamps to SQLite.
1243+
1244+
The default converters are registered under the name "date" for
1245+
:class:`datetime.date` and under the name "timestamp" for
1246+
:class:`datetime.datetime`.
1247+
1248+
This way, you can use date/timestamps from Python without any additional
1249+
fiddling in most cases. The format of the adapters is also compatible with the
1250+
experimental SQLite date/time functions.
1251+
1252+
The following example demonstrates this.
1253+
1254+
.. literalinclude:: ../includes/sqlite3/pysqlite_datetime.py
1255+
1256+
If a timestamp stored in SQLite has a fractional part longer than 6
1257+
numbers, its value will be truncated to microsecond precision by the
1258+
timestamp converter.
1259+
1260+
.. note::
1261+
1262+
The default "timestamp" converter ignores UTC offsets in the database and
1263+
always returns a naive :class:`datetime.datetime` object. To preserve UTC
1264+
offsets in timestamps, either leave converters disabled, or register an
1265+
offset-aware converter with :func:`register_converter`.
1266+
1267+
12361268
.. _sqlite3-howtos:
12371269

12381270
How-to guides
@@ -1270,8 +1302,8 @@ both styles:
12701302

12711303
.. _sqlite3-adapters:
12721304

1273-
Using adapters to store custom Python types in SQLite databases
1274-
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1305+
How to adapt custom Python types to SQLite values
1306+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
12751307

12761308
SQLite supports only a limited set of data types natively.
12771309
To store custom Python types in SQLite databases, *adapt* them to one of the
@@ -1288,8 +1320,8 @@ registering custom adapter functions.
12881320

12891321
.. _sqlite3-conform:
12901322

1291-
Letting your object adapt itself
1292-
""""""""""""""""""""""""""""""""
1323+
How to write adaptable objects
1324+
""""""""""""""""""""""""""""""
12931325

12941326
Suppose we have a :class:`!Point` class that represents a pair of coordinates,
12951327
``x`` and ``y``, in a Cartesian coordinate system.
@@ -1302,8 +1334,8 @@ The object passed to *protocol* will be of type :class:`PrepareProtocol`.
13021334
.. literalinclude:: ../includes/sqlite3/adapter_point_1.py
13031335

13041336

1305-
Registering an adapter callable
1306-
"""""""""""""""""""""""""""""""
1337+
How to register adapter callables
1338+
"""""""""""""""""""""""""""""""""
13071339

13081340
The other possibility is to create a function that converts the Python object
13091341
to an SQLite-compatible type.
@@ -1314,8 +1346,8 @@ This function can then be registered using :func:`register_adapter`.
13141346

13151347
.. _sqlite3-converters:
13161348

1317-
Converting SQLite values to custom Python types
1318-
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1349+
How to convert SQLite values to custom Python types
1350+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
13191351

13201352
Writing an adapter lets you convert *from* custom Python types *to* SQLite
13211353
values.
@@ -1354,36 +1386,6 @@ The following example illustrates the implicit and explicit approaches:
13541386
.. literalinclude:: ../includes/sqlite3/converter_point.py
13551387

13561388

1357-
Default adapters and converters
1358-
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1359-
1360-
There are default adapters for the date and datetime types in the datetime
1361-
module. They will be sent as ISO dates/ISO timestamps to SQLite.
1362-
1363-
The default converters are registered under the name "date" for
1364-
:class:`datetime.date` and under the name "timestamp" for
1365-
:class:`datetime.datetime`.
1366-
1367-
This way, you can use date/timestamps from Python without any additional
1368-
fiddling in most cases. The format of the adapters is also compatible with the
1369-
experimental SQLite date/time functions.
1370-
1371-
The following example demonstrates this.
1372-
1373-
.. literalinclude:: ../includes/sqlite3/pysqlite_datetime.py
1374-
1375-
If a timestamp stored in SQLite has a fractional part longer than 6
1376-
numbers, its value will be truncated to microsecond precision by the
1377-
timestamp converter.
1378-
1379-
.. note::
1380-
1381-
The default "timestamp" converter ignores UTC offsets in the database and
1382-
always returns a naive :class:`datetime.datetime` object. To preserve UTC
1383-
offsets in timestamps, either leave converters disabled, or register an
1384-
offset-aware converter with :func:`register_converter`.
1385-
1386-
13871389
.. _sqlite3-adapter-converter-recipes:
13881390

13891391
Adapter and converter recipes
@@ -1431,8 +1433,8 @@ This section shows recipes for common adapters and converters.
14311433
14321434
.. _sqlite3-connection-shortcuts:
14331435

1434-
Using connection shortcut methods
1435-
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1436+
How to use connection shortcut methods
1437+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
14361438

14371439
Using the :meth:`~Connection.execute`,
14381440
:meth:`~Connection.executemany`, and :meth:`~Connection.executescript`
@@ -1448,7 +1450,7 @@ directly using only a single call on the :class:`Connection` object.
14481450

14491451
.. _sqlite3-connection-context-manager:
14501452

1451-
Using the connection as a context manager
1453+
How to use the connection context manager
14521454
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
14531455

14541456
A :class:`Connection` object can be used as a context manager that
@@ -1473,8 +1475,8 @@ the context manager is a no-op.
14731475

14741476
.. _sqlite3-uri-tricks:
14751477

1476-
Working with SQLite URIs
1477-
^^^^^^^^^^^^^^^^^^^^^^^^
1478+
How to work with SQLite URIs
1479+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
14781480

14791481
Some useful URI tricks include:
14801482

0 commit comments

Comments
 (0)