@@ -360,14 +360,24 @@ Connection Objects
360
360
361
361
.. attribute :: isolation_level
362
362
363
- Get or set the current default isolation level. :const: `None ` for autocommit mode or
364
- one of "DEFERRED", "IMMEDIATE" or "EXCLUSIVE". See section
365
- :ref: `sqlite3-controlling-transactions ` for a more detailed explanation.
363
+ This attribute controls the :ref: `transaction handling
364
+ <sqlite3-controlling-transactions>` performed by ``sqlite3 ``.
365
+ If set to :const: `None `, transactions are never implicitly opened.
366
+ If set to one of ``"DEFERRED" ``, ``"IMMEDIATE" ``, or ``"EXCLUSIVE" ``,
367
+ corresponding to the underlying `SQLite transaction behaviour `_,
368
+ implicit :ref: `transaction management
369
+ <sqlite3-controlling-transactions>` is performed.
370
+
371
+ If not overridden by the *isolation_level * parameter of :func: `connect `,
372
+ the default is ``"" ``, which is an alias for ``"DEFERRED" ``.
366
373
367
374
.. attribute :: in_transaction
368
375
376
+ This read-only attribute corresponds to the low-level SQLite
377
+ `autocommit mode `_.
378
+
369
379
:const: `True ` if a transaction is active (there are uncommitted changes),
370
- :const: `False ` otherwise. Read-only attribute.
380
+ :const: `False ` otherwise.
371
381
372
382
.. versionadded :: 3.2
373
383
@@ -695,21 +705,27 @@ Cursor Objects
695
705
696
706
.. method :: execute(sql[, parameters])
697
707
698
- Executes an SQL statement. Values may be bound to the statement using
708
+ Execute an SQL statement. Values may be bound to the statement using
699
709
:ref: `placeholders <sqlite3-placeholders >`.
700
710
701
711
:meth: `execute ` will only execute a single SQL statement. If you try to execute
702
712
more than one statement with it, it will raise a :exc: `Warning `. Use
703
713
:meth: `executescript ` if you want to execute multiple SQL statements with one
704
714
call.
705
715
716
+ If :attr: `~Connection.isolation_level ` is not :const: `None `,
717
+ *sql * is an ``INSERT ``, ``UPDATE ``, ``DELETE ``, or ``REPLACE `` statement,
718
+ and there is no open transaction,
719
+ a transaction is implicitly opened before executing *sql *.
720
+
706
721
707
722
.. method :: executemany(sql, seq_of_parameters)
708
723
709
- Executes a :ref: `parameterized <sqlite3-placeholders >` SQL command
724
+ Execute a :ref: `parameterized <sqlite3-placeholders >` SQL command
710
725
against all parameter sequences or mappings found in the sequence
711
- *seq_of_parameters *. The :mod: ` sqlite3 ` module also allows using an
726
+ *seq_of_parameters *. It is also possible to use an
712
727
:term: `iterator ` yielding parameters instead of a sequence.
728
+ Uses the same implicit transaction handling as :meth: `~Cursor.execute `.
713
729
714
730
.. literalinclude :: ../includes/sqlite3/executemany_1.py
715
731
@@ -720,12 +736,13 @@ Cursor Objects
720
736
721
737
.. method :: executescript(sql_script)
722
738
723
- This is a nonstandard convenience method for executing multiple SQL statements
724
- at once. It issues a ``COMMIT `` statement first, then executes the SQL script it
725
- gets as a parameter. This method disregards :attr: `isolation_level `; any
726
- transaction control must be added to *sql_script *.
739
+ Execute multiple SQL statements at once.
740
+ If there is a pending transaciton,
741
+ an implicit ``COMMIT `` statement is executed first.
742
+ No other implicit transaction control is performed;
743
+ any transaction control must be added to *sql_script *.
727
744
728
- *sql_script * can be an instance of :class: `str `.
745
+ *sql_script * must be a :class: `string < str> `.
729
746
730
747
Example:
731
748
@@ -1183,38 +1200,43 @@ This section shows recipes for common adapters and converters.
1183
1200
Controlling Transactions
1184
1201
------------------------
1185
1202
1186
- The underlying ``sqlite3 `` library operates in ``autocommit `` mode by default,
1187
- but the Python :mod: `sqlite3 ` module by default does not.
1188
-
1189
- ``autocommit `` mode means that statements that modify the database take effect
1190
- immediately. A ``BEGIN `` or ``SAVEPOINT `` statement disables ``autocommit ``
1191
- mode, and a ``COMMIT ``, a ``ROLLBACK ``, or a ``RELEASE `` that ends the
1192
- outermost transaction, turns ``autocommit `` mode back on.
1193
-
1194
- The Python :mod: `sqlite3 ` module by default issues a ``BEGIN `` statement
1195
- implicitly before a Data Modification Language (DML) statement (i.e.
1196
- ``INSERT ``/``UPDATE ``/``DELETE ``/``REPLACE ``).
1197
-
1198
- You can control which kind of ``BEGIN `` statements :mod: `sqlite3 ` implicitly
1199
- executes via the *isolation_level * parameter to the :func: `connect `
1200
- call, or via the :attr: `isolation_level ` property of connections.
1201
- If you specify no *isolation_level *, a plain ``BEGIN `` is used, which is
1202
- equivalent to specifying ``DEFERRED ``. Other possible values are ``IMMEDIATE ``
1203
- and ``EXCLUSIVE ``.
1204
-
1205
- You can disable the :mod: `sqlite3 ` module's implicit transaction management by
1206
- setting :attr: `isolation_level ` to ``None ``. This will leave the underlying
1207
- ``sqlite3 `` library operating in ``autocommit `` mode. You can then completely
1208
- control the transaction state by explicitly issuing ``BEGIN ``, ``ROLLBACK ``,
1209
- ``SAVEPOINT ``, and ``RELEASE `` statements in your code.
1210
-
1211
- Note that :meth: `~Cursor.executescript ` disregards
1212
- :attr: `isolation_level `; any transaction control must be added explicitly.
1203
+ The ``sqlite3 `` module does not adhere to the transaction handling recommended
1204
+ by :pep: `249 `.
1205
+
1206
+ If the connection attribute :attr: `~Connection.isolation_level `
1207
+ is not :const: `None `,
1208
+ new transactions are implicitly opened before
1209
+ :meth: `~Cursor.execute ` and :meth: `~Cursor.executemany ` executes
1210
+ ``INSERT ``, ``UPDATE ``, ``DELETE ``, or ``REPLACE `` statements.
1211
+ Use the :meth: `~Connection.commit ` and :meth: `~Connection.rollback ` methods
1212
+ to respectively commit and roll back pending transactions.
1213
+ You can choose the underlying `SQLite transaction behaviour `_ —
1214
+ that is, whether and what type of ``BEGIN `` statements ``sqlite3 ``
1215
+ implicitly executes –
1216
+ via the :attr: `~Connection.isolation_level ` attribute.
1217
+
1218
+ If :attr: `~Connection.isolation_level ` is set to :const: `None `,
1219
+ no transactions are implicitly opened at all.
1220
+ This leaves the underlying SQLite library in `autocommit mode `_,
1221
+ but also allows the user to perform their own transaction handling
1222
+ using explicit SQL statements.
1223
+ The underlying SQLite library autocommit mode can be queried using the
1224
+ :attr: `~Connection.in_transaction ` attribute.
1225
+
1226
+ The :meth: `~Cursor.executescript ` method implicitly commits
1227
+ any pending transaction before execution of the given SQL script,
1228
+ regardless of the value of :attr: `~Connection.isolation_level `.
1213
1229
1214
1230
.. versionchanged :: 3.6
1215
1231
:mod: `sqlite3 ` used to implicitly commit an open transaction before DDL
1216
1232
statements. This is no longer the case.
1217
1233
1234
+ .. _autocommit mode :
1235
+ https://www.sqlite.org/lang_transaction.html#implicit_versus_explicit_transactions
1236
+
1237
+ .. _SQLite transaction behaviour :
1238
+ https://www.sqlite.org/lang_transaction.html#deferred_immediate_and_exclusive_transactions
1239
+
1218
1240
1219
1241
Using :mod: `sqlite3 ` efficiently
1220
1242
--------------------------------
0 commit comments