From 7d5982173bfabee2ee8ef1c5f8f279bbcc4c69f9 Mon Sep 17 00:00:00 2001 From: pranamika Date: Wed, 1 Apr 2020 12:38:53 +0530 Subject: [PATCH] Fixed issue caused by converting SQL insert queries to f-strings without proper variable conversion (#62) resolved --- databaseconnect.py | 54 ++++++++++++++++++++-------------------------- 1 file changed, 23 insertions(+), 31 deletions(-) diff --git a/databaseconnect.py b/databaseconnect.py index 00e412f..f42df2a 100644 --- a/databaseconnect.py +++ b/databaseconnect.py @@ -57,9 +57,8 @@ def setup_database(): def add_to_database(classification, subject, root, verb, H): db = connection_to_database() cur = db.cursor() - cur = db.cursor(buffered=True) + cur = db.cursor(prepared=True) if classification == 'C': - #cur.execute(f"INSERT INTO chat_table(root_word,verb,sentence) VALUES ('{root}','{verb}','{H}')") cur.execute("INSERT INTO chat_table(root_word,verb,sentence) VALUES (%s, %s, %s)", (str(root), str(verb), H)) db.commit() elif classification == 'Q': @@ -72,8 +71,7 @@ def add_to_database(classification, subject, root, verb, H): break if exist == 0: # do not add if question already exists - #cur.execute(f"INSERT INTO question_table(subject,root_word,verb,sentence) VALUES ('{subject}','{root}','{verb}','{H}')") - cur.execute("INSERT INTO question_table(subject,root_word,verb,sentence) VALUES (%s,%s,%s,%s)", (str(subject), str(root),str(verb),H)) + cur.execute("INSERT INTO question_table(subject,root_word,verb,sentence) VALUES (%s,%s,%s,%s)", (str(subject[0]), str(root),str(verb),H)) db.commit() else: cur.execute("SELECT sentence FROM statement_table") @@ -84,8 +82,7 @@ def add_to_database(classification, subject, root, verb, H): exist = 1 break if exist == 0: # do not add if question already exists - #cur.execute(f"INSERT INTO statement_table(subject,root_word,verb,sentence) VALUES ('{subject}','{root}','{verb}','{H}')") - cur.execute("INSERT INTO statement_table(subject,root_word,verb,sentence) VALUES (%s,%s,%s,%s)", (str(subject), str(root), str(verb), H)) + cur.execute("INSERT INTO statement_table(subject,root_word,verb,sentence) VALUES (%s,%s,%s,%s)", (str(subject[0]), str(root), str(verb), H)) db.commit() @@ -100,7 +97,7 @@ def get_chat_response(): total_chat_records = res[0] import random chat_id = random.randint(1, total_chat_records+1) - cur.execute(f"SELECT sentence FROM chat_table WHERE id = {chat_id}") + cur.execute("SELECT sentence FROM chat_table WHERE id = '{}'".format(chat_id)) res = cur.fetchone() B = res[0] return B @@ -119,8 +116,7 @@ def get_question_response(subject, root, verb): found = 1 break if found == 1: - cur.execute(f"SELECT sentence FROM statement_table WHERE verb='{verb}'") - #cur.execute("SELECT sentence FROM statement_table WHERE verb= %s", str(verb)) + cur.execute("SELECT sentence FROM statement_table WHERE verb= '{}'".format(verb)) res = cur.fetchone() B = res[0] return B, 0 @@ -132,23 +128,20 @@ def get_question_response(subject, root, verb): res = cur.fetchall() found = 0 for r in res: - if r[-1] == str(subject): + if r[-1] == str(subject[0]): found = 1 break if found == 1: - #cur.execute(f"SELECT verb FROM statement_table WHERE subject='{subject}'") cur.execute("SELECT verb FROM statement_table WHERE subject= '{}'".format(subject[0])) res = cur.fetchone() checkVerb = res[0] # checkVerb is a string while verb is a list. checkVerb ['verb'] if checkVerb == '[]': - #cur.execute(f"SELECT sentence FROM statement_table WHERE subject='{subject}'") cur.execute("SELECT sentence FROM statement_table WHERE subject= '{}'".format(subject[0])) res = cur.fetchone() B = res[0] return B, 0 else: if checkVerb[2:-2] == verb[0]: - #cur.execute(f"SELECT sentence FROM statement_table WHERE subject='{subject}'") cur.execute("SELECT sentence FROM statement_table WHERE subject= '{}'".format(subject[0])) res = cur.fetchone() B = res[0] @@ -165,7 +158,6 @@ def get_question_response(subject, root, verb): def add_learnt_statement_to_database(subject, root, verb): db = connection_to_database() cur = db.cursor() - #cur.execute(f"INSERT INTO statement_table(subject,root_word,verb) VALUES ('{subject}','{root}','{verb}')") cur.execute("INSERT INTO statement_table(subject,root_word,verb) VALUES (%s,%s,%s)", (str(subject), str(root), str(verb))) db.commit() @@ -177,53 +169,53 @@ def learn_question_response(H): cur.execute("SELECT id FROM statement_table ORDER BY id DESC") res = cur.fetchone() last_id = res[0] - #cur.execute(f"UPDATE statement_table SET sentence='{H}' WHERE id={last_id}") + #cur.execute("UPDATE statement_table SET sentence={H} WHERE id={last_id}") cur.execute("UPDATE statement_table SET sentence = '{}' WHERE id = '{}'".format(H, last_id)) db.commit() B = "Thank you! I have learnt this." return B, 0 - +@logger_config.logger def clear_table(table_name): db = connection_to_database() cur = db.cursor() if table_name in ("question_table", "statement_table"): tables_to_be_cleaned = ("question_table", "statement_table") - print("The following tables will be cleaned:\n") + cur.execute("The following tables will be cleaned:\n") for table in tables_to_be_cleaned: describe_table(cur, table) if input("Enter 'Y' to confirm cleaning of BOTH tables: ") in ("Y", "y"): for table in tables_to_be_cleaned: - cur.execute(f"DELETE FROM {table}") + cur.execute("DELETE FROM {table}") db.commit() - print("Tables cleaned successfully") + cur.execute("Tables cleaned successfully") else: - print("Table cleaning skipped.") + cur.execute("Table cleaning skipped.") else: - print("The following table will be cleaned:\n") + cur.execute("The following table will be cleaned:\n") describe_table(cur, table_name) if input("Enter 'Y' to confirm: ") in ("Y", "y"): - print("Table cleaned successfully") - cur.execute(f"DELETE FROM {table_name}") + cur.execute("Table cleaned successfully") + cur.execute("DELETE FROM {table_name}") db.commit() else: - print("Table cleaning skipped.") - + cur.execute("Table cleaning skipped.") +@logger_config.logger def describe_table(cur, table_name): - cur.execute(f"DESC {table_name}") + cur.execute("DESC {table_name}") res = cur.fetchall() column_names = [col[0] for col in res] - cur.execute(f"SELECT COUNT(*) FROM {table_name}") + cur.execute("SELECT COUNT(*) FROM {table_name}") res = cur.fetchall() records_no = res[0][0] - print("Table Name:", table_name) - print("Columns:", column_names) - print("Number of existing records:", records_no) - print() + cur.execute("Table Name:", table_name) + cur.execute("Columns:", column_names) + cur.execute("Number of existing records:", records_no) + cur.execute()