@@ -243,6 +243,7 @@ inserted data and retrieved values from it in multiple ways.
243243 * :ref: `sqlite3-converters `
244244 * :ref: `sqlite3-connection-context-manager `
245245 * :ref: `sqlite3-howto-row-factory `
246+ * :ref: `sqlite3-howto-pragma-in-transaction `
246247
247248 * :ref: `sqlite3-explanation ` for in-depth background on transaction control.
248249
@@ -1353,8 +1354,6 @@ Connection objects
13531354 implying that :mod: `!sqlite3 ` ensures a transaction is always open.
13541355 Use :meth: `commit ` and :meth: `rollback ` to close transactions.
13551356
1356- This is the recommended value of :attr: `!autocommit `.
1357-
13581357 * ``True ``: Use SQLite's `autocommit mode `_.
13591358 :meth: `commit ` and :meth: `rollback ` have no effect in this mode.
13601359
@@ -2429,6 +2428,34 @@ the context manager does nothing.
24292428 couldn't add Python twice
24302429
24312430
2431+ .. _sqlite3-howto-pragma-in-transaction :
2432+
2433+ How to use ``PRAGMA `` statements in transactions
2434+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2435+
2436+ Some ``PRAGMA `` statements have no effect within transactions.
2437+ For example, a ``PRAGMA foreign_keys=ON `` query will silently fail
2438+ if there is an open transaction.
2439+ In order to safely use ``PRAGMA `` statements,
2440+ ensure that there is no open transaction:
2441+
2442+ * If :attr: `~Connection.autocommit ` is ``True ``
2443+ and a transaction was explicitly opened,
2444+ make sure to :meth: `~Cursor.execute ` a ``COMMIT ``
2445+ before executing the ``PRAGMA `` statement.
2446+ * Else, temporarily disable implicit transaction control
2447+ using the :attr: `!autocommit ` attribute
2448+ before executing the ``PRAGMA `` statement:
2449+
2450+ .. testcode ::
2451+
2452+ saved = cur.autocommit
2453+ conn.autocommit = True # Disable implicit transaction control.
2454+ cursor.execute("PRAGMA foreign_keys=ON")
2455+ conn.autocommit = saved # Restore the previous setting.
2456+
2457+
2458+
24322459.. _sqlite3-uri-tricks :
24332460
24342461How to work with SQLite URIs
@@ -2646,8 +2673,7 @@ the :attr:`Connection.autocommit` attribute,
26462673which should preferably be set using the *autocommit * parameter
26472674of :func: `connect `.
26482675
2649- It is suggested to set *autocommit * to ``False ``,
2650- which implies :pep: `249 `-compliant transaction control.
2676+ Set *autocommit * to ``False `` for :pep: `249 `-compliant transaction control.
26512677This means:
26522678
26532679* :mod: `!sqlite3 ` ensures that a transaction is always open,
@@ -2660,6 +2686,11 @@ This means:
26602686* An implicit rollback is performed if the database is
26612687 :meth: `~Connection.close `-ed with pending changes.
26622688
2689+ .. note ::
2690+
2691+ Some ``PRAGMA `` statements cannot be set when a transaction is open.
2692+ See :ref: `sqlite3-howto-pragma-in-transaction ` for details.
2693+
26632694Set *autocommit * to ``True `` to enable SQLite's `autocommit mode `_.
26642695In this mode, :meth: `Connection.commit ` and :meth: `Connection.rollback `
26652696have no effect.
@@ -2716,6 +2747,11 @@ The :meth:`~Cursor.executescript` method implicitly commits
27162747any pending transaction before execution of the given SQL script,
27172748regardless of the value of :attr: `~Connection.isolation_level `.
27182749
2750+ .. note ::
2751+
2752+ Some ``PRAGMA `` statements cannot be set when a transaction is open.
2753+ See :ref: `sqlite3-howto-pragma-in-transaction ` for details.
2754+
27192755.. versionchanged :: 3.6
27202756 :mod: `!sqlite3 ` used to implicitly commit an open transaction before DDL
27212757 statements. This is no longer the case.
0 commit comments