Skip to content

Transactions

SQLiter supports transactions using Python's context manager:

with db:
    db.insert(User(name="Alice", age=30, email="[email protected]"))
    db.insert(User(name="Bob", age=35, email="[email protected]"))
    # If an exception occurs, the transaction will be rolled back

Using the context manager will automatically commit the transaction at the end (unless an exception occurs), regardless of the auto_commit setting. If an exception occurs, all changes made within the transaction block are rolled back.

Warning

Breaking change in 0.21.0: the context manager no longer closes the connection on exit. Use close() explicitly when you are finished with the database instance.