diff --git a/databaseconnect.py b/databaseconnect.py index a8fe13b..d3cda47 100644 --- a/databaseconnect.py +++ b/databaseconnect.py @@ -54,9 +54,10 @@ 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}')") + print(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': cur.execute("SELECT sentence FROM question_table") @@ -68,7 +69,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)) db.commit() else: cur.execute("SELECT sentence FROM statement_table") @@ -79,7 +80,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)) db.commit() @logger_config.logger @@ -111,7 +112,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(f"SELECT sentence FROM statement_table WHERE verb={verb}") res = cur.fetchone() B = res[0] return B,0 @@ -127,17 +128,17 @@ def get_question_response(subject,root,verb): found = 1 break if found == 1: - cur.execute(f"SELECT verb FROM statement_table WHERE subject='{subject}'") + cur.execute(f"SELECT verb FROM statement_table WHERE subject={subject}") 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(f"SELECT sentence FROM statement_table WHERE subject={subject}") 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(f"SELECT sentence FROM statement_table WHERE subject={subject}") res = cur.fetchone() B = res[0] return B,0 @@ -152,7 +153,7 @@ 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() @logger_config.logger @@ -162,11 +163,12 @@ 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(f"UPDATE statement_table SET sentence={H} WHERE id={last_id}") db.commit() B = "Thank you! I have learnt this." return B,0 + def clear_table(table_name): db = connection_to_database() cur = db.cursor() @@ -196,6 +198,7 @@ def clear_table(table_name): else: print("Table cleaning skipped.") +@@logger_config.logger def describe_table(cursor, table_name): cur.execute(f"DESC {table_name}") res = cur.fetchall()