You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Add context manager support for database connections (#96)
Implements __enter__ and __exit__ methods to enable using Connection
objects as context managers. On clean exit, transactions are
automatically committed. On exception, transactions are automatically
rolled back. This provides sqlite3-compatible behavior and safer
transaction handling.
Closes#95
Copy file name to clipboardExpand all lines: docs/api.md
+10Lines changed: 10 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -32,6 +32,16 @@ Rolls back the current transaction and starts a new one.
32
32
33
33
Closes the database connection.
34
34
35
+
### `with` statement
36
+
37
+
Connection objects can be used as context managers to ensure that transactions are properly committed or rolled back. When entering the context, the connection object is returned. When exiting:
38
+
- Without exception: automatically commits the transaction
39
+
- With exception: automatically rolls back the transaction
40
+
41
+
This behavior is compatible with Python's `sqlite3` module. Context managers work correctly in both transactional and autocommit modes.
42
+
43
+
When mixing manual transaction control with context managers, the context manager's commit/rollback will apply to any active transaction at the time of exit. Manual calls to `commit()` or `rollback()` within the context are allowed and will start a new transaction as usual.
44
+
35
45
### execute(sql, parameters=())
36
46
37
47
Create a new cursor object and executes the SQL statement.
0 commit comments