Skip to content

Commit

Permalink
Closing db connections
Browse files Browse the repository at this point in the history
  • Loading branch information
SGA-pranamika-pandey committed Jan 24, 2024
1 parent fff4562 commit 4b1f98f
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions databaseconnect.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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
Expand All @@ -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()
Expand Down Expand Up @@ -132,18 +133,20 @@ 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
# get a random chat response
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")
Expand All @@ -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


Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 4b1f98f

Please sign in to comment.