Skip to content

Commit

Permalink
PR related to Bugfix : Fix issue caused by converting SQL insert quer…
Browse files Browse the repository at this point in the history
…ies to f-strings without proper variable conversion vishakha-lall#62
  • Loading branch information
Pihu1998 committed Mar 22, 2020
1 parent 47de45b commit 4067ef0
Showing 1 changed file with 13 additions and 10 deletions.
23 changes: 13 additions & 10 deletions databaseconnect.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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")
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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()
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit 4067ef0

Please sign in to comment.