Skip to content

Commit b942f64

Browse files
Add documentation
1 parent 6ca9045 commit b942f64

File tree

1 file changed

+88
-15
lines changed

1 file changed

+88
-15
lines changed

Doc/library/sqlite3.rst

Lines changed: 88 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ Module functions and constants
236236
the preceding space: the column name would simply be "Expiration date".
237237

238238

239-
.. function:: connect(database[, timeout, detect_types, isolation_level, check_same_thread, factory, cached_statements, uri])
239+
.. function:: connect(database[, timeout, detect_types, isolation_level, check_same_thread, factory, cached_statements, uri, autocommit])
240240

241241
Opens a connection to the SQLite database file *database*. By default returns a
242242
:class:`Connection` object, unless a custom *factory* is given.
@@ -255,6 +255,11 @@ Module functions and constants
255255
For the *isolation_level* parameter, please see the
256256
:attr:`~Connection.isolation_level` property of :class:`Connection` objects.
257257

258+
For the *autocommit* parameter, please see the
259+
:attr:`~Connection.autocommit` property. *autocommit* currently defaults to
260+
:data:`~Connection.DEPRECATED_TRANSACTION_CONTROL`.
261+
The default will change to :const:`False` in a future Python release.
262+
258263
SQLite natively supports only the types TEXT, INTEGER, REAL, BLOB and NULL. If
259264
you want to use other types you must add support for them yourself. The
260265
*detect_types* parameter and the using custom **converters** registered with the
@@ -318,6 +323,9 @@ Module functions and constants
318323
.. versionchanged:: 3.10
319324
Added the ``sqlite3.connect/handle`` auditing event.
320325

326+
.. versionchanged:: 3.12
327+
Added the *autocommit* parameter.
328+
321329

322330
.. function:: register_converter(typename, callable)
323331

@@ -385,6 +393,25 @@ Connection Objects
385393

386394
An SQLite database connection has the following attributes and methods:
387395

396+
.. attribute:: autocommit
397+
398+
Get or set the :pep:`249` transaction behaviour.
399+
*autocommit* has tree allowed values:
400+
401+
* :const:`False`: PEP 249 compliant transaction behaviour,
402+
implying that a transaction is always open.
403+
Use :meth:`commit` and :meth:`rollback` to close transactions.
404+
Closing a transaction immediately opens a new one.
405+
This will be the default value of *autocommit* in a future Python
406+
release.
407+
408+
* :const:`True`: Use SQLite's autocommit behaviour.
409+
You are also free to :meth:`execute` custom transaction statements.
410+
411+
* :data:`DEPRECATED_TRANSACTION_CONTROL`: Pre Python 3.12 compliant
412+
transaction control. See :attr:`isolation_level`.
413+
This is currently the default value of *autocommit*.
414+
388415
.. attribute:: isolation_level
389416

390417
Get or set the current default isolation level. :const:`None` for autocommit mode or
@@ -1365,41 +1392,83 @@ timestamp converter.
13651392

13661393
.. _sqlite3-controlling-transactions:
13671394

1368-
Controlling Transactions
1369-
------------------------
1395+
Controlling Transactions Using the Autocommit Property
1396+
------------------------------------------------------
13701397

13711398
The underlying ``sqlite3`` library operates in ``autocommit`` mode by default,
13721399
but the Python :mod:`sqlite3` module by default does not.
13731400

1374-
``autocommit`` mode means that statements that modify the database take effect
1401+
Use the :attr:`~Connection.autocommit` property to select transaction mode.
1402+
This attribute can also be set when :meth:`connecting <~Connection.connect>`.
1403+
1404+
Currently, *autocommit* defaults to :const:`DEPRECATED_TRANSACTION_CONTROL`,
1405+
which means transaction control is selected using the
1406+
:attr:`~Connection.isolation_level` property.
1407+
See :ref:`sqlite3-deprecated-transaction-control` for more information.
1408+
1409+
In a future Python release, *autocommit* will default to :const:False,
1410+
which will imply :pep:`249` compliant transaction control. This means:
1411+
1412+
* A transaction is always open.
1413+
* Commit transactions using :meth:`~Connection.commit`.
1414+
* Roll back transactions using :meth:`~Connection.rollback`.
1415+
* Commit and rollback will open a new transaction immediately after execution.
1416+
* An implicit rollback is performed if the database is closed with pending
1417+
changes.
1418+
1419+
Set *autocommit* to :const:`True` to enable SQLite's autocommit mode.
1420+
This mode is equal to setting :attr:`~Connection.isolation_level` to
1421+
:const:`None`.
1422+
1423+
``autocommit=True`` means that statements that modify the database take effect
13751424
immediately. A ``BEGIN`` or ``SAVEPOINT`` statement disables ``autocommit``
13761425
mode, and a ``COMMIT``, a ``ROLLBACK``, or a ``RELEASE`` that ends the
13771426
outermost transaction, turns ``autocommit`` mode back on.
13781427

1379-
The Python :mod:`sqlite3` module by default issues a ``BEGIN`` statement
1380-
implicitly before a Data Modification Language (DML) statement (i.e.
1381-
``INSERT``/``UPDATE``/``DELETE``/``REPLACE``).
13821428

1383-
You can control which kind of ``BEGIN`` statements :mod:`sqlite3` implicitly
1429+
.. _sqlite3-deprecated-transaction-control:
1430+
1431+
Controlling Transactions Using the Isolation Level Property
1432+
-----------------------------------------------------------
1433+
1434+
.. note::
1435+
1436+
The recommended way of controlling transactions, is via the
1437+
:attr:`~Connection.autocommit` parameter.
1438+
1439+
If :attr:`~Connection.autocommit` is set to
1440+
:data:`DEPRECATED_TRANSACTION_CONTROL`, transaction control is selected using
1441+
the :attr:`~Connection.isolation_level` parameter.
1442+
1443+
``isolation_level`` defaults to the empty string: ``""``. This implies that
1444+
the Python :mod:`sqlite3` module issues a ``BEGIN`` statement
1445+
implicitly before a Data Modification Language (DML) statement
1446+
(``INSERT``, ``UPDATE``, ``DELETE``, and ``REPLACE``).
1447+
1448+
You can control the kind of ``BEGIN`` statement ``sqlite3`` implicitly
13841449
executes via the *isolation_level* parameter to the :func:`connect`
1385-
call, or via the :attr:`isolation_level` property of connections.
1386-
If you specify no *isolation_level*, a plain ``BEGIN`` is used, which is
1387-
equivalent to specifying ``DEFERRED``. Other possible values are ``IMMEDIATE``
1388-
and ``EXCLUSIVE``.
1450+
call, or via the :attr:`isolation_level` property of connection objects.
1451+
By default, ``BEGIN`` is used, which is equivalent to
1452+
``isolation_level="DEFERRED"``.
1453+
Other possible values are ``"IMMEDIATE"`` and ``"EXCLUSIVE"``.
13891454

1390-
You can disable the :mod:`sqlite3` module's implicit transaction management by
1455+
Disable the :mod:`sqlite3` module's implicit transaction control by
13911456
setting :attr:`isolation_level` to ``None``. This will leave the underlying
13921457
``sqlite3`` library operating in ``autocommit`` mode. You can then completely
1393-
control the transaction state by explicitly issuing ``BEGIN``, ``ROLLBACK``,
1458+
control transactions by explicitly executing ``BEGIN``, ``ROLLBACK``,
13941459
``SAVEPOINT``, and ``RELEASE`` statements in your code.
13951460

13961461
Note that :meth:`~Cursor.executescript` disregards
13971462
:attr:`isolation_level`; any transaction control must be added explicitly.
13981463

13991464
.. versionchanged:: 3.6
1400-
:mod:`sqlite3` used to implicitly commit an open transaction before DDL
1465+
``sqlite3`` used to implicitly commit an open transaction before DDL
14011466
statements. This is no longer the case.
14021467

1468+
.. versionchanged: 3.12
1469+
The recommended way of controlling transactions is now via the
1470+
:attr:`~Connection.autocommit` parameter.
1471+
14031472
14041473
Using :mod:`sqlite3` efficiently
14051474
--------------------------------
@@ -1441,6 +1510,10 @@ committed:
14411510

14421511
.. literalinclude:: ../includes/sqlite3/ctx_manager.py
14431512

1513+
.. note::
1514+
1515+
The context manager does not implicitly begin a new transaction.
1516+
14441517

14451518
.. rubric:: Footnotes
14461519

0 commit comments

Comments
 (0)