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