@@ -236,7 +236,7 @@ Module functions and constants
236
236
the preceding space: the column name would simply be "Expiration date".
237
237
238
238
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 ])
240
240
241
241
Opens a connection to the SQLite database file *database *. By default returns a
242
242
:class: `Connection ` object, unless a custom *factory * is given.
@@ -255,6 +255,11 @@ Module functions and constants
255
255
For the *isolation_level * parameter, please see the
256
256
:attr: `~Connection.isolation_level ` property of :class: `Connection ` objects.
257
257
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
+
258
263
SQLite natively supports only the types TEXT, INTEGER, REAL, BLOB and NULL. If
259
264
you want to use other types you must add support for them yourself. The
260
265
*detect_types * parameter and the using custom **converters ** registered with the
@@ -318,6 +323,9 @@ Module functions and constants
318
323
.. versionchanged :: 3.10
319
324
Added the ``sqlite3.connect/handle `` auditing event.
320
325
326
+ .. versionchanged :: 3.12
327
+ Added the *autocommit * parameter.
328
+
321
329
322
330
.. function :: register_converter(typename, callable)
323
331
@@ -385,6 +393,25 @@ Connection Objects
385
393
386
394
An SQLite database connection has the following attributes and methods:
387
395
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
+
388
415
.. attribute :: isolation_level
389
416
390
417
Get or set the current default isolation level. :const: `None ` for autocommit mode or
@@ -1365,41 +1392,83 @@ timestamp converter.
1365
1392
1366
1393
.. _sqlite3-controlling-transactions :
1367
1394
1368
- Controlling Transactions
1369
- ------------------------
1395
+ Controlling Transactions Using the Autocommit Property
1396
+ ------------------------------------------------------
1370
1397
1371
1398
The underlying ``sqlite3 `` library operates in ``autocommit `` mode by default,
1372
1399
but the Python :mod: `sqlite3 ` module by default does not.
1373
1400
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
1375
1424
immediately. A ``BEGIN `` or ``SAVEPOINT `` statement disables ``autocommit ``
1376
1425
mode, and a ``COMMIT ``, a ``ROLLBACK ``, or a ``RELEASE `` that ends the
1377
1426
outermost transaction, turns ``autocommit `` mode back on.
1378
1427
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 ``).
1382
1428
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
1384
1449
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" ``.
1389
1454
1390
- You can disable the :mod: `sqlite3 ` module's implicit transaction management by
1455
+ Disable the :mod: `sqlite3 ` module's implicit transaction control by
1391
1456
setting :attr: `isolation_level ` to ``None ``. This will leave the underlying
1392
1457
``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 ``,
1394
1459
``SAVEPOINT ``, and ``RELEASE `` statements in your code.
1395
1460
1396
1461
Note that :meth: `~Cursor.executescript ` disregards
1397
1462
:attr: `isolation_level `; any transaction control must be added explicitly.
1398
1463
1399
1464
.. 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
1401
1466
statements. This is no longer the case.
1402
1467
1468
+ .. versionchanged: 3.12
1469
+ The recommended way of controlling transactions is now via the
1470
+ :attr:`~Connection.autocommit` parameter.
1471
+
1403
1472
1404
1473
Using :mod: `sqlite3 ` efficiently
1405
1474
--------------------------------
@@ -1441,6 +1510,10 @@ committed:
1441
1510
1442
1511
.. literalinclude :: ../includes/sqlite3/ctx_manager.py
1443
1512
1513
+ .. note ::
1514
+
1515
+ The context manager does not implicitly begin a new transaction.
1516
+
1444
1517
1445
1518
.. rubric :: Footnotes
1446
1519
0 commit comments