mirror of
https://github.com/simonw/sqlite-utils.git
synced 2026-07-23 09:24:31 +02:00
Add db.begin(), db.commit() and db.rollback() methods
Manual transaction control previously required mixing raw
db.execute("begin") strings with methods on db.conn. These thin
wrappers give the documentation and callers a single consistent
idiom. Docs updated to use them throughout.
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01UnLnhsH25Nnv7LHhekUfPd
This commit is contained in:
parent
6c88067ab7
commit
788c393a30
5 changed files with 75 additions and 6 deletions
|
|
@ -455,6 +455,31 @@ class Database:
|
|||
self.conn.rollback()
|
||||
raise
|
||||
|
||||
def begin(self) -> None:
|
||||
"""
|
||||
Start a transaction with ``BEGIN``, taking manual control of transaction
|
||||
handling. End it by calling :meth:`commit` or :meth:`rollback`.
|
||||
|
||||
Raises ``sqlite3.OperationalError`` if a transaction is already open.
|
||||
|
||||
Most code should use the :meth:`atomic` context manager instead, which
|
||||
commits and rolls back automatically. See :ref:`python_api_transactions`.
|
||||
"""
|
||||
self.execute("BEGIN")
|
||||
|
||||
def commit(self) -> None:
|
||||
"""
|
||||
Commit the current transaction. Does nothing if no transaction is open.
|
||||
"""
|
||||
self.conn.commit()
|
||||
|
||||
def rollback(self) -> None:
|
||||
"""
|
||||
Roll back the current transaction, discarding its changes. Does nothing
|
||||
if no transaction is open.
|
||||
"""
|
||||
self.conn.rollback()
|
||||
|
||||
@contextlib.contextmanager
|
||||
def ensure_autocommit_off(self) -> Generator[None, None, None]:
|
||||
"""
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue