Skip to content

Commit

Permalink
Fixed issue caused by converting SQL insert queries to f-strings with…
Browse files Browse the repository at this point in the history
…out proper variable conversion (vishakha-lall#62) resolved
  • Loading branch information
Pihu1998 committed Apr 1, 2020
1 parent 9b19101 commit 7d59821
Showing 1 changed file with 23 additions and 31 deletions.
54 changes: 23 additions & 31 deletions databaseconnect.py
Original file line number Diff line number Diff line change
Expand Up @@ -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':
Expand All @@ -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")
Expand All @@ -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()


Expand All @@ -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
Expand All @@ -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
Expand All @@ -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]
Expand All @@ -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()

Expand All @@ -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()

0 comments on commit 7d59821

Please sign in to comment.