Skip to content

Commit fd5b09f

Browse files
authored
Database as a context manager, fixed many pytest warnings
* Database can now work as a context manager * Claude Code helped fix a ton of .close() warnings https://gistpreview.github.io/?730f0c5dc38528a1dd0615f330bd5481 * New autouse fixture to help with test warnings Refs #692 (comment) * Fix all remaining resource warnings https://gistpreview.github.io/?0bb8e869b82f6ff0db647de755182502 Closes #692
1 parent 066d0f3 commit fd5b09f

File tree

12 files changed

+231
-66
lines changed

12 files changed

+231
-66
lines changed

docs/python-api.rst

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,27 @@ You can pass ``strict=True`` to enable `SQLite STRICT mode <https://www.sqlite.o
123123
124124
db = Database("my_database.db", strict=True)
125125
126+
.. _python_api_close:
127+
128+
Closing a database
129+
------------------
130+
131+
Database objects maintain a connection to the underlying SQLite database. You can explicitly close this connection using the ``.close()`` method:
132+
133+
.. code-block:: python
134+
135+
db = Database("my_database.db")
136+
# ... use the database ...
137+
db.close()
138+
139+
The ``Database`` object also works as a context manager, which will automatically close the connection when the ``with`` block exits:
140+
141+
.. code-block:: python
142+
143+
with Database("my_database.db") as db:
144+
db["my_table"].insert({"name": "Example"})
145+
# Connection is automatically closed here
146+
126147
.. _python_api_attach:
127148

128149
Attaching additional databases

0 commit comments

Comments
 (0)