From db7326f86b95f3d7bcc4ea7c7c1dd2123fba5fcf Mon Sep 17 00:00:00 2001 From: Fabian Joswig Date: Mon, 5 May 2025 17:00:39 +0200 Subject: [PATCH] [Fix] Use sqlite3 context managers in pandas module. --- pyerrors/input/pandas.py | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/pyerrors/input/pandas.py b/pyerrors/input/pandas.py index 13482983..7519e9c5 100644 --- a/pyerrors/input/pandas.py +++ b/pyerrors/input/pandas.py @@ -29,9 +29,8 @@ def to_sql(df, table_name, db, if_exists='fail', gz=True, **kwargs): None """ se_df = _serialize_df(df, gz=gz) - con = sqlite3.connect(db) - se_df.to_sql(table_name, con, if_exists=if_exists, index=False, **kwargs) - con.close() + with sqlite3.connect(db) as con: + se_df.to_sql(table_name, con=con, if_exists=if_exists, index=False, **kwargs) def read_sql(sql, db, auto_gamma=False, **kwargs): @@ -52,9 +51,8 @@ def read_sql(sql, db, auto_gamma=False, **kwargs): data : pandas.DataFrame Dataframe with the content of the sqlite database. """ - con = sqlite3.connect(db) - extract_df = pd.read_sql(sql, con, **kwargs) - con.close() + with sqlite3.connect(db) as con: + extract_df = pd.read_sql(sql, con=con, **kwargs) return _deserialize_df(extract_df, auto_gamma=auto_gamma)