@@ -25,6 +25,15 @@ The sqlite3 module was written by Gerhard Häring. It provides an SQL interface
25
25
compliant with the DB-API 2.0 specification described by :pep: `249 `, and
26
26
requires SQLite 3.7.15 or newer.
27
27
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
+
28
37
29
38
.. _sqlite3-tutorial :
30
39
@@ -136,10 +145,15 @@ both styles:
136
145
PEP written by Marc-André Lemburg.
137
146
138
147
148
+ .. _sqlite3-reference :
149
+
150
+ Reference
151
+ ---------
152
+
139
153
.. _sqlite3-module-contents :
140
154
141
155
Module functions and constants
142
- ------------------------------
156
+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
143
157
144
158
145
159
.. data :: apilevel
@@ -421,8 +435,8 @@ Module functions and constants
421
435
422
436
.. _sqlite3-connection-objects :
423
437
424
- Connection Objects
425
- ------------------
438
+ Connection objects
439
+ ^^^^^^^^^^^^^^^^^^
426
440
427
441
.. class :: Connection
428
442
@@ -982,8 +996,8 @@ Connection Objects
982
996
983
997
.. _sqlite3-cursor-objects :
984
998
985
- Cursor Objects
986
- --------------
999
+ Cursor objects
1000
+ ^^^^^^^^^^^^^^
987
1001
988
1002
A ``Cursor `` object represents a `database cursor `_
989
1003
which is used to execute SQL statements,
@@ -1159,8 +1173,8 @@ Cursor Objects
1159
1173
1160
1174
.. _sqlite3-row-objects :
1161
1175
1162
- Row Objects
1163
- -----------
1176
+ Row objects
1177
+ ^^^^^^^^^^^
1164
1178
1165
1179
.. class :: Row
1166
1180
@@ -1224,8 +1238,8 @@ Now we plug :class:`Row` in::
1224
1238
1225
1239
.. _sqlite3-blob-objects :
1226
1240
1227
- Blob Objects
1228
- ------------
1241
+ Blob objects
1242
+ ^^^^^^^^^^^^
1229
1243
1230
1244
.. versionadded :: 3.11
1231
1245
@@ -1276,8 +1290,8 @@ Blob Objects
1276
1290
end).
1277
1291
1278
1292
1279
- PrepareProtocol Objects
1280
- -----------------------
1293
+ PrepareProtocol objects
1294
+ ^^^^^^^^^^^^^^^^^^^^^^^
1281
1295
1282
1296
.. class :: PrepareProtocol
1283
1297
@@ -1289,7 +1303,7 @@ PrepareProtocol Objects
1289
1303
.. _sqlite3-exceptions :
1290
1304
1291
1305
Exceptions
1292
- ----------
1306
+ ^^^^^^^^^^
1293
1307
1294
1308
The exception hierarchy is defined by the DB-API 2.0 (:pep: `249 `).
1295
1309
@@ -1379,11 +1393,7 @@ The exception hierarchy is defined by the DB-API 2.0 (:pep:`249`).
1379
1393
.. _sqlite3-types :
1380
1394
1381
1395
SQLite and Python types
1382
- -----------------------
1383
-
1384
-
1385
- Introduction
1386
- ^^^^^^^^^^^^
1396
+ ^^^^^^^^^^^^^^^^^^^^^^^
1387
1397
1388
1398
SQLite natively supports the following types: ``NULL ``, ``INTEGER ``,
1389
1399
``REAL ``, ``TEXT ``, ``BLOB ``.
@@ -1423,10 +1433,18 @@ This is how SQLite types are converted to Python types by default:
1423
1433
+-------------+----------------------------------------------+
1424
1434
1425
1435
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 :
1429
1443
1444
+ How-to guides
1445
+ -------------
1446
+
1447
+ .. _sqlite3-adapters :
1430
1448
1431
1449
Using adapters to store custom Python types in SQLite databases
1432
1450
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -1549,7 +1567,7 @@ The deprecated default adapters and converters consist of:
1549
1567
1550
1568
.. _sqlite3-adapter-converter-recipes :
1551
1569
1552
- Adapter and Converter Recipes
1570
+ Adapter and converter recipes
1553
1571
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1554
1572
1555
1573
This section shows recipes for common adapters and converters.
@@ -1592,83 +1610,6 @@ This section shows recipes for common adapters and converters.
1592
1610
sqlite3.register_converter("timestamp", convert_timestamp)
1593
1611
1594
1612
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
-
1672
1613
.. _sqlite3-connection-shortcuts :
1673
1614
1674
1615
Using connection shortcut methods
@@ -1686,6 +1627,8 @@ directly using only a single call on the :class:`Connection` object.
1686
1627
.. literalinclude :: ../includes/sqlite3/shortcut_methods.py
1687
1628
1688
1629
1630
+ .. _sqlite3-columns-by-name :
1631
+
1689
1632
Accessing columns by name instead of by index
1690
1633
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1691
1634
@@ -1721,3 +1664,82 @@ the context manager is a no-op.
1721
1664
nor closes the connection.
1722
1665
1723
1666
.. 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