From 4b1f98fe7857d3db7b4f289e03701507550376d2 Mon Sep 17 00:00:00 2001 From: SGA-pranamika-pandey Date: Wed, 24 Jan 2024 14:58:12 +0000 Subject: [PATCH] Closing db connections --- databaseconnect.py | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/databaseconnect.py b/databaseconnect.py index 1e9020b..36c4e5e 100644 --- a/databaseconnect.py +++ b/databaseconnect.py @@ -53,7 +53,7 @@ def setup_database() -> object: Setup database with chat table, question table, statement table and directions table Returns: - Database object + None """ db = connection_to_database() cursor = db.cursor() @@ -72,7 +72,8 @@ def setup_database() -> object: cursor.execute( "CREATE TABLE IF NOT EXISTS {TABLES[3]}(id INTEGER PRIMARY KEY AUTO_INCREMENT, origin_location VARCHAR(100), destination_location VARCHAR(100))" # noqa: E501 ) - return db + cursor.close() + db.close() @logger_config.logger @@ -88,7 +89,7 @@ def add_to_database(classification: str, subject: str, root: str, verb: str, sen verb (str): Verb relation of sentence from user input. Returns: - db (object): Database object after adding data + None """ db = connection_to_database() @@ -132,7 +133,8 @@ def add_to_database(classification: str, subject: str, root: str, verb: str, sen params = (str(subject), str(root), str(verb), sentence,) cursor.execute(query, params) db.commit() - return db + cursor.close() + db.close() @logger_config.logger @@ -140,10 +142,11 @@ def add_to_database(classification: str, subject: str, root: str, verb: str, sen def get_chat_response() -> str: """ Gets a random chat response from the chat table. + Returns: response(str): A chat response from the chat table. """ - + db = connection_to_database() cursor = db.cursor(prepared=True) cursor.execute("SELECT COUNT(*) FROM chat_table") @@ -156,6 +159,8 @@ def get_chat_response() -> str: cursor.execute(query, params) result = cursor.fetchone() response = result[0] + cursor.close() + db.close() return response @@ -256,6 +261,8 @@ def learn_question_response(sentence: str) -> tuple[str, str]: params = (sentence, str(last_id),) cursor.execute(query, params) db.commit() + cursor.close() + db.close() response = "Thank you! I have learnt this." return response, chatbot.LearnResponse.MESSAGE.name @@ -299,7 +306,8 @@ def clear_table(table_name) -> object: else: logging.debug("Table cleaning skipped.") - return db + cursor.close() + db.close() @logger_config.logger