@@ -396,14 +396,24 @@ Connection Objects
396
396
397
397
.. attribute :: isolation_level
398
398
399
- Get or set the current default isolation level. :const: `None ` for autocommit mode or
400
- one of "DEFERRED", "IMMEDIATE" or "EXCLUSIVE". See section
401
- :ref: `sqlite3-controlling-transactions ` for a more detailed explanation.
399
+ This attribute controls the :ref: `transaction handling
400
+ <sqlite3-controlling-transactions>` performed by ``sqlite3 ``.
401
+ If set to :const: `None `, transactions are never implicitly opened.
402
+ If set to one of ``"DEFERRED" ``, ``"IMMEDIATE" ``, or ``"EXCLUSIVE" ``,
403
+ corresponding to the underlying `SQLite transaction behaviour `_,
404
+ implicit :ref: `transaction management
405
+ <sqlite3-controlling-transactions>` is performed.
406
+
407
+ If not overridden by the *isolation_level * parameter of :func: `connect `,
408
+ the default is ``"" ``, which is an alias for ``"DEFERRED" ``.
402
409
403
410
.. attribute :: in_transaction
404
411
412
+ This read-only attribute corresponds to the low-level SQLite
413
+ `autocommit mode `_.
414
+
405
415
:const: `True ` if a transaction is active (there are uncommitted changes),
406
- :const: `False ` otherwise. Read-only attribute.
416
+ :const: `False ` otherwise.
407
417
408
418
.. versionadded :: 3.2
409
419
@@ -858,21 +868,27 @@ Cursor Objects
858
868
859
869
.. method :: execute(sql[, parameters])
860
870
861
- Executes an SQL statement. Values may be bound to the statement using
871
+ Execute an SQL statement. Values may be bound to the statement using
862
872
:ref: `placeholders <sqlite3-placeholders >`.
863
873
864
874
:meth: `execute ` will only execute a single SQL statement. If you try to execute
865
875
more than one statement with it, it will raise a :exc: `ProgrammingError `. Use
866
876
:meth: `executescript ` if you want to execute multiple SQL statements with one
867
877
call.
868
878
879
+ If :attr: `~Connection.isolation_level ` is not :const: `None `,
880
+ *sql * is an ``INSERT ``, ``UPDATE ``, ``DELETE ``, or ``REPLACE `` statement,
881
+ and there is no open transaction,
882
+ a transaction is implicitly opened before executing *sql *.
883
+
869
884
870
885
.. method :: executemany(sql, seq_of_parameters)
871
886
872
- Executes a :ref: `parameterized <sqlite3-placeholders >` SQL command
887
+ Execute a :ref: `parameterized <sqlite3-placeholders >` SQL command
873
888
against all parameter sequences or mappings found in the sequence
874
- *seq_of_parameters *. The :mod: ` sqlite3 ` module also allows using an
889
+ *seq_of_parameters *. It is also possible to use an
875
890
:term: `iterator ` yielding parameters instead of a sequence.
891
+ Uses the same implicit transaction handling as :meth: `~Cursor.execute `.
876
892
877
893
.. literalinclude :: ../includes/sqlite3/executemany_1.py
878
894
@@ -883,12 +899,13 @@ Cursor Objects
883
899
884
900
.. method :: executescript(sql_script)
885
901
886
- This is a nonstandard convenience method for executing multiple SQL statements
887
- at once. It issues a ``COMMIT `` statement first, then executes the SQL script it
888
- gets as a parameter. This method disregards :attr: `isolation_level `; any
889
- transaction control must be added to *sql_script *.
902
+ Execute multiple SQL statements at once.
903
+ If there is a pending transaciton,
904
+ an implicit ``COMMIT `` statement is executed first.
905
+ No other implicit transaction control is performed;
906
+ any transaction control must be added to *sql_script *.
890
907
891
- *sql_script * can be an instance of :class: `str `.
908
+ *sql_script * must be a :class: `string < str> `.
892
909
893
910
Example:
894
911
@@ -1415,38 +1432,43 @@ This section shows recipes for common adapters and converters.
1415
1432
Controlling Transactions
1416
1433
------------------------
1417
1434
1418
- The underlying ``sqlite3 `` library operates in ``autocommit `` mode by default,
1419
- but the Python :mod: `sqlite3 ` module by default does not.
1420
-
1421
- ``autocommit `` mode means that statements that modify the database take effect
1422
- immediately. A ``BEGIN `` or ``SAVEPOINT `` statement disables ``autocommit ``
1423
- mode, and a ``COMMIT ``, a ``ROLLBACK ``, or a ``RELEASE `` that ends the
1424
- outermost transaction, turns ``autocommit `` mode back on.
1425
-
1426
- The Python :mod: `sqlite3 ` module by default issues a ``BEGIN `` statement
1427
- implicitly before a Data Modification Language (DML) statement (i.e.
1428
- ``INSERT ``/``UPDATE ``/``DELETE ``/``REPLACE ``).
1429
-
1430
- You can control which kind of ``BEGIN `` statements :mod: `sqlite3 ` implicitly
1431
- executes via the *isolation_level * parameter to the :func: `connect `
1432
- call, or via the :attr: `isolation_level ` property of connections.
1433
- If you specify no *isolation_level *, a plain ``BEGIN `` is used, which is
1434
- equivalent to specifying ``DEFERRED ``. Other possible values are ``IMMEDIATE ``
1435
- and ``EXCLUSIVE ``.
1436
-
1437
- You can disable the :mod: `sqlite3 ` module's implicit transaction management by
1438
- setting :attr: `isolation_level ` to ``None ``. This will leave the underlying
1439
- ``sqlite3 `` library operating in ``autocommit `` mode. You can then completely
1440
- control the transaction state by explicitly issuing ``BEGIN ``, ``ROLLBACK ``,
1441
- ``SAVEPOINT ``, and ``RELEASE `` statements in your code.
1442
-
1443
- Note that :meth: `~Cursor.executescript ` disregards
1444
- :attr: `isolation_level `; any transaction control must be added explicitly.
1435
+ The ``sqlite3 `` module does not adhere to the transaction handling recommended
1436
+ by :pep: `249 `.
1437
+
1438
+ If the connection attribute :attr: `~Connection.isolation_level `
1439
+ is not :const: `None `,
1440
+ new transactions are implicitly opened before
1441
+ :meth: `~Cursor.execute ` and :meth: `~Cursor.executemany ` executes
1442
+ ``INSERT ``, ``UPDATE ``, ``DELETE ``, or ``REPLACE `` statements.
1443
+ Use the :meth: `~Connection.commit ` and :meth: `~Connection.rollback ` methods
1444
+ to respectively commit and roll back pending transactions.
1445
+ You can choose the underlying `SQLite transaction behaviour `_ —
1446
+ that is, whether and what type of ``BEGIN `` statements ``sqlite3 ``
1447
+ implicitly executes –
1448
+ via the :attr: `~Connection.isolation_level ` attribute.
1449
+
1450
+ If :attr: `~Connection.isolation_level ` is set to :const: `None `,
1451
+ no transactions are implicitly opened at all.
1452
+ This leaves the underlying SQLite library in `autocommit mode `_,
1453
+ but also allows the user to perform their own transaction handling
1454
+ using explicit SQL statements.
1455
+ The underlying SQLite library autocommit mode can be queried using the
1456
+ :attr: `~Connection.in_transaction ` attribute.
1457
+
1458
+ The :meth: `~Cursor.executescript ` method implicitly commits
1459
+ any pending transaction before execution of the given SQL script,
1460
+ regardless of the value of :attr: `~Connection.isolation_level `.
1445
1461
1446
1462
.. versionchanged :: 3.6
1447
1463
:mod: `sqlite3 ` used to implicitly commit an open transaction before DDL
1448
1464
statements. This is no longer the case.
1449
1465
1466
+ .. _autocommit mode :
1467
+ https://www.sqlite.org/lang_transaction.html#implicit_versus_explicit_transactions
1468
+
1469
+ .. _SQLite transaction behaviour :
1470
+ https://www.sqlite.org/lang_transaction.html#deferred_immediate_and_exclusive_transactions
1471
+
1450
1472
1451
1473
Using :mod: `sqlite3 ` efficiently
1452
1474
--------------------------------
0 commit comments