I've been trying to place variables inside my SQL command. However, when I try this:
def interact_database(command):
connection = psycopg2.connect("dbname=NAME user=NAME password=PASSWORD")
cursor = connection.cursor()
cursor.execute(command)
connection.commit()
results = None
try:
results = cursor.fetchall()
except psycopg2.ProgrammingError:
print("Connection failure.")
pass
cursor.close()
connection.close()
return results
def pick_question(type):
if type == 'G':
QuestionID = random.randint(1,38)
elif type == 'Gr':
QuestionID = random.randint(39,60)
elif type == 'R':
QuestionID = random.randint(61,89)
else:
QuestionID = random.randint(90,119)
interact_database("SELECT Question FROM Questions WHERE Question_ID = %s")(QuestionID)
pick_question('G')
I get this error
psycopg2.ProgrammingError: syntax error at or near "%"
LINE 1: SELECT Question FROM Questions WHERE Question_ID = %s
I've tried Googling it multiple times, but everywhere I read this should work. Does someone know what I'm doing wrong here? Thanks in advance!