Database can now work as a context manager, refs #692

This commit is contained in:
Simon Willison 2025-12-11 16:22:22 -08:00
commit f77ca0ec0d
2 changed files with 27 additions and 0 deletions

View file

@ -382,6 +382,12 @@ class Database:
pm.hook.prepare_connection(conn=self.conn)
self.strict = strict
def __enter__(self) -> "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()