@@ -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
@@ -411,8 +425,8 @@ Module functions and constants
411
425
412
426
.. _sqlite3-connection-objects :
413
427
414
- Connection Objects
415
- ------------------
428
+ Connection objects
429
+ ^^^^^^^^^^^^^^^^^^
416
430
417
431
.. class :: Connection
418
432
@@ -972,8 +986,8 @@ Connection Objects
972
986
973
987
.. _sqlite3-cursor-objects :
974
988
975
- Cursor Objects
976
- --------------
989
+ Cursor objects
990
+ ^^^^^^^^^^^^^^
977
991
978
992
A ``Cursor `` object represents a `database cursor `_
979
993
which is used to execute SQL statements,
@@ -1149,8 +1163,8 @@ Cursor Objects
1149
1163
1150
1164
.. _sqlite3-row-objects :
1151
1165
1152
- Row Objects
1153
- -----------
1166
+ Row objects
1167
+ ^^^^^^^^^^^
1154
1168
1155
1169
.. class :: Row
1156
1170
@@ -1212,8 +1226,10 @@ Now we plug :class:`Row` in::
1212
1226
35.14
1213
1227
1214
1228
1215
- Blob Objects
1216
- ------------
1229
+ .. _sqlite3-blob-objects :
1230
+
1231
+ Blob objects
1232
+ ^^^^^^^^^^^^
1217
1233
1218
1234
.. versionadded :: 3.11
1219
1235
@@ -1264,8 +1280,8 @@ Blob Objects
1264
1280
end).
1265
1281
1266
1282
1267
- PrepareProtocol Objects
1268
- -----------------------
1283
+ PrepareProtocol objects
1284
+ ^^^^^^^^^^^^^^^^^^^^^^^
1269
1285
1270
1286
.. class :: PrepareProtocol
1271
1287
@@ -1277,7 +1293,7 @@ PrepareProtocol Objects
1277
1293
.. _sqlite3-exceptions :
1278
1294
1279
1295
Exceptions
1280
- ----------
1296
+ ^^^^^^^^^^
1281
1297
1282
1298
The exception hierarchy is defined by the DB-API 2.0 (:pep: `249 `).
1283
1299
@@ -1364,16 +1380,10 @@ The exception hierarchy is defined by the DB-API 2.0 (:pep:`249`).
1364
1380
``NotSupportedError `` is a subclass of :exc: `DatabaseError `.
1365
1381
1366
1382
1367
- .. _sqlite3-blob-objects :
1368
-
1369
1383
.. _sqlite3-types :
1370
1384
1371
1385
SQLite and Python types
1372
- -----------------------
1373
-
1374
-
1375
- Introduction
1376
- ^^^^^^^^^^^^
1386
+ ^^^^^^^^^^^^^^^^^^^^^^^
1377
1387
1378
1388
SQLite natively supports the following types: ``NULL ``, ``INTEGER ``,
1379
1389
``REAL ``, ``TEXT ``, ``BLOB ``.
@@ -1413,10 +1423,18 @@ This is how SQLite types are converted to Python types by default:
1413
1423
+-------------+----------------------------------------------+
1414
1424
1415
1425
The type system of the :mod: `sqlite3 ` module is extensible in two ways: you can
1416
- store additional Python types in an SQLite database via object adaptation, and
1417
- you can let the :mod: `sqlite3 ` module convert SQLite types to different Python
1418
- types via converters.
1426
+ store additional Python types in an SQLite database via
1427
+ :ref: `object adapters <sqlite3-adapters >`,
1428
+ and you can let the ``sqlite3 `` module convert SQLite types to
1429
+ Python types via :ref: `converters <sqlite3-converters >`.
1430
+
1419
1431
1432
+ .. _sqlite3-howtos :
1433
+
1434
+ How-to guides
1435
+ -------------
1436
+
1437
+ .. _sqlite3-adapters :
1420
1438
1421
1439
Using adapters to store custom Python types in SQLite databases
1422
1440
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -1460,6 +1478,8 @@ This function can then be registered using :func:`register_adapter`.
1460
1478
.. literalinclude :: ../includes/sqlite3/adapter_point_2.py
1461
1479
1462
1480
1481
+ .. _sqlite3-converters :
1482
+
1463
1483
Converting SQLite values to custom Python types
1464
1484
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1465
1485
@@ -1532,7 +1552,7 @@ timestamp converter.
1532
1552
1533
1553
.. _sqlite3-adapter-converter-recipes :
1534
1554
1535
- Adapter and Converter Recipes
1555
+ Adapter and converter recipes
1536
1556
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1537
1557
1538
1558
This section shows recipes for common adapters and converters.
@@ -1575,83 +1595,6 @@ This section shows recipes for common adapters and converters.
1575
1595
sqlite3.register_converter("timestamp", convert_timestamp)
1576
1596
1577
1597
1578
- .. _sqlite3-controlling-transactions :
1579
-
1580
- Controlling Transactions
1581
- ------------------------
1582
-
1583
- The ``sqlite3 `` module does not adhere to the transaction handling recommended
1584
- by :pep: `249 `.
1585
-
1586
- If the connection attribute :attr: `~Connection.isolation_level `
1587
- is not :const: `None `,
1588
- new transactions are implicitly opened before
1589
- :meth: `~Cursor.execute ` and :meth: `~Cursor.executemany ` executes
1590
- ``INSERT ``, ``UPDATE ``, ``DELETE ``, or ``REPLACE `` statements.
1591
- Use the :meth: `~Connection.commit ` and :meth: `~Connection.rollback ` methods
1592
- to respectively commit and roll back pending transactions.
1593
- You can choose the underlying `SQLite transaction behaviour `_ —
1594
- that is, whether and what type of ``BEGIN `` statements ``sqlite3 ``
1595
- implicitly executes –
1596
- via the :attr: `~Connection.isolation_level ` attribute.
1597
-
1598
- If :attr: `~Connection.isolation_level ` is set to :const: `None `,
1599
- no transactions are implicitly opened at all.
1600
- This leaves the underlying SQLite library in `autocommit mode `_,
1601
- but also allows the user to perform their own transaction handling
1602
- using explicit SQL statements.
1603
- The underlying SQLite library autocommit mode can be queried using the
1604
- :attr: `~Connection.in_transaction ` attribute.
1605
-
1606
- The :meth: `~Cursor.executescript ` method implicitly commits
1607
- any pending transaction before execution of the given SQL script,
1608
- regardless of the value of :attr: `~Connection.isolation_level `.
1609
-
1610
- .. versionchanged :: 3.6
1611
- :mod: `sqlite3 ` used to implicitly commit an open transaction before DDL
1612
- statements. This is no longer the case.
1613
-
1614
- .. _autocommit mode :
1615
- https://www.sqlite.org/lang_transaction.html#implicit_versus_explicit_transactions
1616
-
1617
- .. _SQLite transaction behaviour :
1618
- https://www.sqlite.org/lang_transaction.html#deferred_immediate_and_exclusive_transactions
1619
-
1620
-
1621
- .. _sqlite3-uri-tricks :
1622
-
1623
- SQLite URI tricks
1624
- -----------------
1625
-
1626
- Some useful URI tricks include:
1627
-
1628
- * Open a database in read-only mode::
1629
-
1630
- con = sqlite3.connect("file:template.db?mode=ro", uri=True)
1631
-
1632
- * Do not implicitly create a new database file if it does not already exist;
1633
- will raise :exc: `~sqlite3.OperationalError ` if unable to create a new file::
1634
-
1635
- con = sqlite3.connect("file:nosuchdb.db?mode=rw", uri=True)
1636
-
1637
- * Create a shared named in-memory database::
1638
-
1639
- con1 = sqlite3.connect("file:mem1?mode=memory&cache=shared", uri=True)
1640
- con2 = sqlite3.connect("file:mem1?mode=memory&cache=shared", uri=True)
1641
- con1.execute("create table t(t)")
1642
- con1.execute("insert into t values(28)")
1643
- con1.commit()
1644
- rows = con2.execute("select * from t").fetchall()
1645
-
1646
- More information about this feature, including a list of parameters,
1647
- can be found in the `SQLite URI documentation `_.
1648
-
1649
- .. _SQLite URI documentation : https://www.sqlite.org/uri.html
1650
-
1651
- Using :mod: `sqlite3 ` efficiently
1652
- --------------------------------
1653
-
1654
-
1655
1598
.. _sqlite3-connection-shortcuts :
1656
1599
1657
1600
Using connection shortcut methods
@@ -1669,6 +1612,8 @@ directly using only a single call on the :class:`Connection` object.
1669
1612
.. literalinclude :: ../includes/sqlite3/shortcut_methods.py
1670
1613
1671
1614
1615
+ .. _sqlite3-columns-by-name :
1616
+
1672
1617
Accessing columns by name instead of by index
1673
1618
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1674
1619
@@ -1704,3 +1649,82 @@ the context manager is a no-op.
1704
1649
nor closes the connection.
1705
1650
1706
1651
.. literalinclude :: ../includes/sqlite3/ctx_manager.py
1652
+
1653
+
1654
+ .. _sqlite3-uri-tricks :
1655
+
1656
+ Working with SQLite URIs
1657
+ ^^^^^^^^^^^^^^^^^^^^^^^^
1658
+
1659
+ Some useful URI tricks include:
1660
+
1661
+ * Open a database in read-only mode::
1662
+
1663
+ con = sqlite3.connect("file:template.db?mode=ro", uri=True)
1664
+
1665
+ * Do not implicitly create a new database file if it does not already exist;
1666
+ will raise :exc: `~sqlite3.OperationalError ` if unable to create a new file::
1667
+
1668
+ con = sqlite3.connect("file:nosuchdb.db?mode=rw", uri=True)
1669
+
1670
+ * Create a shared named in-memory database::
1671
+
1672
+ con1 = sqlite3.connect("file:mem1?mode=memory&cache=shared", uri=True)
1673
+ con2 = sqlite3.connect("file:mem1?mode=memory&cache=shared", uri=True)
1674
+ con1.execute("create table t(t)")
1675
+ con1.execute("insert into t values(28)")
1676
+ con1.commit()
1677
+ rows = con2.execute("select * from t").fetchall()
1678
+
1679
+ More information about this feature, including a list of parameters,
1680
+ can be found in the `SQLite URI documentation `_.
1681
+
1682
+ .. _SQLite URI documentation : https://www.sqlite.org/uri.html
1683
+
1684
+
1685
+ .. _sqlite3-explanation :
1686
+
1687
+ Explanation
1688
+ -----------
1689
+
1690
+ .. _sqlite3-controlling-transactions :
1691
+
1692
+ Transaction control
1693
+ ^^^^^^^^^^^^^^^^^^^
1694
+
1695
+ The ``sqlite3 `` module does not adhere to the transaction handling recommended
1696
+ by :pep: `249 `.
1697
+
1698
+ If the connection attribute :attr: `~Connection.isolation_level `
1699
+ is not :const: `None `,
1700
+ new transactions are implicitly opened before
1701
+ :meth: `~Cursor.execute ` and :meth: `~Cursor.executemany ` executes
1702
+ ``INSERT ``, ``UPDATE ``, ``DELETE ``, or ``REPLACE `` statements.
1703
+ Use the :meth: `~Connection.commit ` and :meth: `~Connection.rollback ` methods
1704
+ to respectively commit and roll back pending transactions.
1705
+ You can choose the underlying `SQLite transaction behaviour `_ —
1706
+ that is, whether and what type of ``BEGIN `` statements ``sqlite3 ``
1707
+ implicitly executes –
1708
+ via the :attr: `~Connection.isolation_level ` attribute.
1709
+
1710
+ If :attr: `~Connection.isolation_level ` is set to :const: `None `,
1711
+ no transactions are implicitly opened at all.
1712
+ This leaves the underlying SQLite library in `autocommit mode `_,
1713
+ but also allows the user to perform their own transaction handling
1714
+ using explicit SQL statements.
1715
+ The underlying SQLite library autocommit mode can be queried using the
1716
+ :attr: `~Connection.in_transaction ` attribute.
1717
+
1718
+ The :meth: `~Cursor.executescript ` method implicitly commits
1719
+ any pending transaction before execution of the given SQL script,
1720
+ regardless of the value of :attr: `~Connection.isolation_level `.
1721
+
1722
+ .. versionchanged :: 3.6
1723
+ :mod: `sqlite3 ` used to implicitly commit an open transaction before DDL
1724
+ statements. This is no longer the case.
1725
+
1726
+ .. _autocommit mode :
1727
+ https://www.sqlite.org/lang_transaction.html#implicit_versus_explicit_transactions
1728
+
1729
+ .. _SQLite transaction behaviour :
1730
+ https://www.sqlite.org/lang_transaction.html#deferred_immediate_and_exclusive_transactions
0 commit comments