@@ -258,7 +258,8 @@ Module functions
258
258
.. function :: connect(database, timeout=5.0, detect_types=0, \
259
259
isolation_level="DEFERRED", check_same_thread=True, \
260
260
factory=sqlite3.Connection, cached_statements=128, \
261
- uri=False)
261
+ uri=False, \* , \
262
+ autocommit=sqlite3.LEGACY_TRANSACTION_CONTROL)
262
263
263
264
Open a connection to an SQLite database.
264
265
@@ -290,11 +291,12 @@ Module functions
290
291
By default (``0 ``), type detection is disabled.
291
292
292
293
:param isolation_level:
293
- The :attr: `~ Connection.isolation_level ` of the connection,
294
- controlling whether and how transactions are implicitly opened .
294
+ See :attr: `Connection.isolation_level ` and
295
+ :ref: ` sqlite3-transaction-control-isolation-level ` for more information .
295
296
Can be ``"DEFERRED" `` (default), ``"EXCLUSIVE" `` or ``"IMMEDIATE" ``;
296
297
or ``None `` to disable opening transactions implicitly.
297
- See :ref: `sqlite3-controlling-transactions ` for more.
298
+ Has no effect unless :attr: `Connection.autocommit ` is set to
299
+ :data: `~sqlite3.LEGACY_TRANSACTION_CONTROL ` (the default).
298
300
:type isolation_level: str | None
299
301
300
302
:param bool check_same_thread:
@@ -321,6 +323,14 @@ Module functions
321
323
The query string allows passing parameters to SQLite,
322
324
enabling various :ref: `sqlite3-uri-tricks `.
323
325
326
+ :param autocommit:
327
+ See :attr: `Connection.autocommit ` and
328
+ :ref: `sqlite3-transaction-control-autocommit ` for more information.
329
+ *autocommit * currently defaults to
330
+ :data: `~sqlite3.LEGACY_TRANSACTION_CONTROL `.
331
+ The default will change to ``False `` in a future Python release.
332
+ :type autocommit: bool
333
+
324
334
:rtype: Connection
325
335
326
336
.. audit-event :: sqlite3.connect database sqlite3.connect
@@ -335,6 +345,9 @@ Module functions
335
345
.. versionadded :: 3.10
336
346
The ``sqlite3.connect/handle `` auditing event.
337
347
348
+ .. versionadded :: 3.12
349
+ The *autocommit * parameter.
350
+
338
351
.. function :: complete_statement(statement)
339
352
340
353
Return ``True `` if the string *statement * appears to contain
@@ -418,6 +431,12 @@ Module functions
418
431
Module constants
419
432
^^^^^^^^^^^^^^^^
420
433
434
+ .. data :: LEGACY_TRANSACTION_CONTROL
435
+
436
+ Set :attr: `~Connection.autocommit ` to this constant to select
437
+ old style (pre-Python 3.12) transaction control behaviour.
438
+ See :ref: `sqlite3-transaction-control-isolation-level ` for more information.
439
+
421
440
.. data :: PARSE_COLNAMES
422
441
423
442
Pass this flag value to the *detect_types * parameter of
@@ -616,18 +635,27 @@ Connection objects
616
635
.. method :: commit()
617
636
618
637
Commit any pending transaction to the database.
619
- If there is no open transaction, this method is a no-op.
638
+ If :attr: `autocommit ` is ``True ``, or there is no open transaction,
639
+ this method does nothing.
640
+ If :attr: `!autocommit ` is ``False ``, a new transaction is implicitly
641
+ opened if a pending transaction was committed by this method.
620
642
621
643
.. method :: rollback()
622
644
623
645
Roll back to the start of any pending transaction.
624
- If there is no open transaction, this method is a no-op.
646
+ If :attr: `autocommit ` is ``True ``, or there is no open transaction,
647
+ this method does nothing.
648
+ If :attr: `!autocommit ` is ``False ``, a new transaction is implicitly
649
+ opened if a pending transaction was rolled back by this method.
625
650
626
651
.. method :: close()
627
652
628
653
Close the database connection.
629
- Any pending transaction is not committed implicitly;
630
- make sure to :meth: `commit ` before closing
654
+ If :attr: `autocommit ` is ``False ``,
655
+ any pending transaction is implicitly rolled back.
656
+ If :attr: `!autocommit ` is ``True `` or :data: `LEGACY_TRANSACTION_CONTROL `,
657
+ no implicit transaction control is executed.
658
+ Make sure to :meth: `commit ` before closing
631
659
to avoid losing pending changes.
632
660
633
661
.. method :: execute(sql, parameters=(), /)
@@ -1224,6 +1252,38 @@ Connection objects
1224
1252
1225
1253
.. versionadded :: 3.11
1226
1254
1255
+ .. attribute :: autocommit
1256
+
1257
+ This attribute controls :pep: `249 `-compliant transaction behaviour.
1258
+ :attr: `!autocommit ` has three allowed values:
1259
+
1260
+ * ``False ``: Select :pep: `249 `-compliant transaction behaviour,
1261
+ implying that :mod: `!sqlite3 ` ensures a transaction is always open.
1262
+ Use :meth: `commit ` and :meth: `rollback ` to close transactions.
1263
+
1264
+ This is the recommended value of :attr: `!autocommit `.
1265
+
1266
+ * ``True ``: Use SQLite's `autocommit mode `_.
1267
+ :meth: `commit ` and :meth: `rollback ` have no effect in this mode.
1268
+
1269
+ * :data: `LEGACY_TRANSACTION_CONTROL `:
1270
+ Pre-Python 3.12 (non-:pep: `249 `-compliant) transaction control.
1271
+ See :attr: `isolation_level ` for more details.
1272
+
1273
+ This is currently the default value of :attr: `!autocommit `.
1274
+
1275
+ Changing :attr: `!autocommit ` to ``False `` will open a new transaction,
1276
+ and changing it to ``True `` will commit any pending transaction.
1277
+
1278
+ See :ref: `sqlite3-transaction-control-autocommit ` for more details.
1279
+
1280
+ .. note ::
1281
+
1282
+ The :attr: `isolation_level ` attribute has no effect unless
1283
+ :attr: `autocommit ` is :data: `LEGACY_TRANSACTION_CONTROL `.
1284
+
1285
+ .. versionadded :: 3.12
1286
+
1227
1287
.. attribute :: in_transaction
1228
1288
1229
1289
This read-only attribute corresponds to the low-level SQLite
@@ -1236,17 +1296,24 @@ Connection objects
1236
1296
1237
1297
.. attribute :: isolation_level
1238
1298
1239
- This attribute controls the :ref: `transaction handling
1240
- <sqlite3-controlling-transactions >` performed by :mod: `!sqlite3 `.
1299
+ Controls the :ref: `legacy transaction handling mode
1300
+ <sqlite3-transaction-control-isolation-level >` of :mod: `!sqlite3 `.
1241
1301
If set to ``None ``, transactions are never implicitly opened.
1242
1302
If set to one of ``"DEFERRED" ``, ``"IMMEDIATE" ``, or ``"EXCLUSIVE" ``,
1243
1303
corresponding to the underlying `SQLite transaction behaviour `_,
1244
- implicit :ref: `transaction management
1245
- <sqlite3-controlling-transactions >` is performed.
1304
+ :ref: `implicit transaction management
1305
+ <sqlite3-transaction-control-isolation-level >` is performed.
1246
1306
1247
1307
If not overridden by the *isolation_level * parameter of :func: `connect `,
1248
1308
the default is ``"" ``, which is an alias for ``"DEFERRED" ``.
1249
1309
1310
+ .. note ::
1311
+
1312
+ Using :attr: `autocommit ` to control transaction handling is
1313
+ recommended over using :attr: `!isolation_level `.
1314
+ :attr: `!isolation_level ` has no effect unless :attr: `autocommit ` is
1315
+ set to :data: `LEGACY_TRANSACTION_CONTROL ` (the default).
1316
+
1250
1317
.. attribute :: row_factory
1251
1318
1252
1319
A callable that accepts two arguments,
@@ -1375,7 +1442,9 @@ Cursor objects
1375
1442
:meth: `executescript ` if you want to execute multiple SQL statements with one
1376
1443
call.
1377
1444
1378
- If :attr: `~Connection.isolation_level ` is not ``None ``,
1445
+ If :attr: `~Connection.autocommit ` is
1446
+ :data: `LEGACY_TRANSACTION_CONTROL `,
1447
+ :attr: `~Connection.isolation_level ` is not ``None ``,
1379
1448
*sql * is an ``INSERT ``, ``UPDATE ``, ``DELETE ``, or ``REPLACE `` statement,
1380
1449
and there is no open transaction,
1381
1450
a transaction is implicitly opened before executing *sql *.
@@ -1403,7 +1472,9 @@ Cursor objects
1403
1472
.. method :: executescript(sql_script, /)
1404
1473
1405
1474
Execute the SQL statements in *sql_script *.
1406
- If there is a pending transaction,
1475
+ If the :attr: `~Connection.autocommit ` is
1476
+ :data: `LEGACY_TRANSACTION_CONTROL `
1477
+ and there is a pending transaction,
1407
1478
an implicit ``COMMIT `` statement is executed first.
1408
1479
No other implicit transaction control is performed;
1409
1480
any transaction control must be added to *sql_script *.
@@ -2202,9 +2273,12 @@ the transaction is committed.
2202
2273
If this commit fails,
2203
2274
or if the body of the ``with `` statement raises an uncaught exception,
2204
2275
the transaction is rolled back.
2276
+ If :attr: `~Connection.autocommit ` is ``False ``,
2277
+ a new transaction is implicitly opened after committing or rolling back.
2205
2278
2206
2279
If there is no open transaction upon leaving the body of the ``with `` statement,
2207
- the context manager is a no-op.
2280
+ or if :attr: `~Connection.autocommit ` is ``True ``,
2281
+ the context manager does nothing.
2208
2282
2209
2283
.. note ::
2210
2284
@@ -2289,13 +2363,72 @@ can be found in the `SQLite URI documentation`_.
2289
2363
Explanation
2290
2364
-----------
2291
2365
2366
+ .. _sqlite3-transaction-control :
2292
2367
.. _sqlite3-controlling-transactions :
2293
2368
2294
2369
Transaction control
2295
2370
^^^^^^^^^^^^^^^^^^^
2296
2371
2297
- The :mod: `!sqlite3 ` module does not adhere to the transaction handling recommended
2298
- by :pep: `249 `.
2372
+ :mod: `!sqlite3 ` offers multiple methods of controlling whether,
2373
+ when and how database transactions are opened and closed.
2374
+ :ref: `sqlite3-transaction-control-autocommit ` is recommended,
2375
+ while :ref: `sqlite3-transaction-control-isolation-level `
2376
+ retains the pre-Python 3.12 behaviour.
2377
+
2378
+ .. _sqlite3-transaction-control-autocommit :
2379
+
2380
+ Transaction control via the ``autocommit `` attribute
2381
+ """"""""""""""""""""""""""""""""""""""""""""""""""""
2382
+
2383
+ The recommended way of controlling transaction behaviour is through
2384
+ the :attr: `Connection.autocommit ` attribute,
2385
+ which should preferrably be set using the *autocommit * parameter
2386
+ of :func: `connect `.
2387
+
2388
+ It is suggested to set *autocommit * to ``False ``,
2389
+ which implies :pep: `249 `-compliant transaction control.
2390
+ This means:
2391
+
2392
+ * :mod: `!sqlite3 ` ensures that a transaction is always open,
2393
+ so :meth: `Connection.commit ` and :meth: `Connection.rollback `
2394
+ will implicitly open a new transaction immediately after closing
2395
+ the pending one.
2396
+ :mod: `!sqlite3 ` uses ``BEGIN DEFERRED `` statements when opening transactions.
2397
+ * Transactions should be committed explicitly using :meth: `!commit `.
2398
+ * Transactions should be rolled back explicitly using :meth: `!rollback `.
2399
+ * An implicit rollback is performed if the database is
2400
+ :meth: `~Connection.close `-ed with pending changes.
2401
+
2402
+ Set *autocommit * to ``True `` to enable SQLite's `autocommit mode `_.
2403
+ In this mode, :meth: `Connection.commit ` and :meth: `Connection.rollback `
2404
+ have no effect.
2405
+ Note that SQLite's autocommit mode is distinct from
2406
+ the :pep: `249 `-compliant :attr: `Connection.autocommit ` attribute;
2407
+ use :attr: `Connection.in_transaction ` to query
2408
+ the low-level SQLite autocommit mode.
2409
+
2410
+ Set *autocommit * to :data: `LEGACY_TRANSACTION_CONTROL `
2411
+ to leave transaction control behaviour to the
2412
+ :attr: `Connection.isolation_level ` attribute.
2413
+ See :ref: `sqlite3-transaction-control-isolation-level ` for more information.
2414
+
2415
+
2416
+ .. _sqlite3-transaction-control-isolation-level :
2417
+
2418
+ Transaction control via the ``isolation_level `` attribute
2419
+ """""""""""""""""""""""""""""""""""""""""""""""""""""""""
2420
+
2421
+ .. note ::
2422
+
2423
+ The recommended way of controlling transactions is via the
2424
+ :attr: `~Connection.autocommit ` attribute.
2425
+ See :ref: `sqlite3-transaction-control-autocommit `.
2426
+
2427
+ If :attr: `Connection.autocommit ` is set to
2428
+ :data: `LEGACY_TRANSACTION_CONTROL ` (the default),
2429
+ transaction behaviour is controlled using
2430
+ the :attr: `Connection.isolation_level ` attribute.
2431
+ Otherwise, :attr: `!isolation_level ` has no effect.
2299
2432
2300
2433
If the connection attribute :attr: `~Connection.isolation_level `
2301
2434
is not ``None ``,
@@ -2326,6 +2459,10 @@ regardless of the value of :attr:`~Connection.isolation_level`.
2326
2459
:mod: `!sqlite3 ` used to implicitly commit an open transaction before DDL
2327
2460
statements. This is no longer the case.
2328
2461
2462
+ .. versionchanged :: 3.12
2463
+ The recommended way of controlling transactions is now via the
2464
+ :attr: `~Connection.autocommit ` attribute.
2465
+
2329
2466
.. _autocommit mode :
2330
2467
https://www.sqlite.org/lang_transaction.html#implicit_versus_explicit_transactions
2331
2468
0 commit comments