From f77ca0ec0db12a6f8c730132ea3b1a10be539902 Mon Sep 17 00:00:00 2001 From: Simon Willison Date: Thu, 11 Dec 2025 16:22:22 -0800 Subject: [PATCH] Database can now work as a context manager, refs #692 --- docs/python-api.rst | 21 +++++++++++++++++++++ sqlite_utils/db.py | 6 ++++++ 2 files changed, 27 insertions(+) diff --git a/docs/python-api.rst b/docs/python-api.rst index f9371d5..267591a 100644 --- a/docs/python-api.rst +++ b/docs/python-api.rst @@ -123,6 +123,27 @@ You can pass ``strict=True`` to enable `SQLite STRICT mode "Database": + return self + + def __exit__(self, exc_type, exc_val, exc_tb) -> None: + self.close() + def close(self) -> None: "Close the SQLite connection, and the underlying database file" self.conn.close()