@@ -1530,6 +1530,38 @@ and you can let the :mod:`!sqlite3` module convert SQLite types to
1530
1530
Python types via :ref: `converters <sqlite3-converters >`.
1531
1531
1532
1532
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
+
1533
1565
.. _sqlite3-howtos :
1534
1566
1535
1567
How-to guides
@@ -1567,8 +1599,8 @@ both styles:
1567
1599
1568
1600
.. _sqlite3-adapters :
1569
1601
1570
- Using adapters to store custom Python types in SQLite databases
1571
- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1602
+ How to adapt custom Python types to SQLite values
1603
+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1572
1604
1573
1605
SQLite supports only a limited set of data types natively.
1574
1606
To store custom Python types in SQLite databases, *adapt * them to one of the
@@ -1585,8 +1617,8 @@ registering custom adapter functions.
1585
1617
1586
1618
.. _sqlite3-conform :
1587
1619
1588
- Letting your object adapt itself
1589
- """"""""""""""""""""""""""""""""
1620
+ How to write adaptable objects
1621
+ """"""""""""""""""""""""""""""
1590
1622
1591
1623
Suppose we have a :class: `!Point ` class that represents a pair of coordinates,
1592
1624
``x `` and ``y ``, in a Cartesian coordinate system.
@@ -1599,8 +1631,8 @@ The object passed to *protocol* will be of type :class:`PrepareProtocol`.
1599
1631
.. literalinclude :: ../includes/sqlite3/adapter_point_1.py
1600
1632
1601
1633
1602
- Registering an adapter callable
1603
- """""""""""""""""""""""""""""""
1634
+ How to register adapter callables
1635
+ """""""""""""""""""""""""""""""""
1604
1636
1605
1637
The other possibility is to create a function that converts the Python object
1606
1638
to an SQLite-compatible type.
@@ -1611,8 +1643,8 @@ This function can then be registered using :func:`register_adapter`.
1611
1643
1612
1644
.. _sqlite3-converters :
1613
1645
1614
- Converting SQLite values to custom Python types
1615
- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1646
+ How to convert SQLite values to custom Python types
1647
+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1616
1648
1617
1649
Writing an adapter lets you convert *from * custom Python types *to * SQLite
1618
1650
values.
@@ -1651,36 +1683,6 @@ The following example illustrates the implicit and explicit approaches:
1651
1683
.. literalinclude :: ../includes/sqlite3/converter_point.py
1652
1684
1653
1685
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
-
1684
1686
.. _sqlite3-adapter-converter-recipes :
1685
1687
1686
1688
Adapter and converter recipes
@@ -1728,8 +1730,8 @@ This section shows recipes for common adapters and converters.
1728
1730
1729
1731
.. _sqlite3-connection-shortcuts :
1730
1732
1731
- Using connection shortcut methods
1732
- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1733
+ How to use connection shortcut methods
1734
+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1733
1735
1734
1736
Using the :meth: `~Connection.execute `,
1735
1737
:meth: `~Connection.executemany `, and :meth: `~Connection.executescript `
@@ -1745,7 +1747,7 @@ directly using only a single call on the :class:`Connection` object.
1745
1747
1746
1748
.. _sqlite3-connection-context-manager :
1747
1749
1748
- Using the connection as a context manager
1750
+ How to use the connection context manager
1749
1751
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1750
1752
1751
1753
A :class: `Connection ` object can be used as a context manager that
@@ -1770,8 +1772,8 @@ the context manager is a no-op.
1770
1772
1771
1773
.. _sqlite3-uri-tricks :
1772
1774
1773
- Working with SQLite URIs
1774
- ^^^^^^^^^^^^^^^^^^^^^^^^
1775
+ How to work with SQLite URIs
1776
+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1775
1777
1776
1778
Some useful URI tricks include:
1777
1779
0 commit comments