mirror of
https://github.com/simonw/sqlite-utils.git
synced 2026-07-23 09:24:31 +02:00
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 https://github.com/simonw/sqlite-utils/issues/692#issuecomment-3644371889 * Fix all remaining resource warnings https://gistpreview.github.io/?0bb8e869b82f6ff0db647de755182502 Closes #692
This commit is contained in:
parent
066d0f3e8d
commit
fd5b09f64b
12 changed files with 231 additions and 66 deletions
|
|
@ -123,6 +123,27 @@ You can pass ``strict=True`` to enable `SQLite STRICT mode <https://www.sqlite.o
|
|||
|
||||
db = Database("my_database.db", strict=True)
|
||||
|
||||
.. _python_api_close:
|
||||
|
||||
Closing a database
|
||||
------------------
|
||||
|
||||
Database objects maintain a connection to the underlying SQLite database. You can explicitly close this connection using the ``.close()`` method:
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
db = Database("my_database.db")
|
||||
# ... use the database ...
|
||||
db.close()
|
||||
|
||||
The ``Database`` object also works as a context manager, which will automatically close the connection when the ``with`` block exits:
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
with Database("my_database.db") as db:
|
||||
db["my_table"].insert({"name": "Example"})
|
||||
# Connection is automatically closed here
|
||||
|
||||
.. _python_api_attach:
|
||||
|
||||
Attaching additional databases
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue