Hi I have the following block of code that is meant to take the variable 'search_value'and pass it into the WHERE clause of a mysql select statement
import MySQLdb
search_term = input('Enter your search term: ')
print (search_term)
conn = MySQLdb.connect(my connection info)
c = conn.cursor()
q = "SELECT * FROM courses WHERE course_area = %(value)s "
params = {'value': search_term}
c.execute(q, params)
rows = c.fetchall()
for eachRow in rows:
print (eachRow)
I know that I need to use %s somewhere but I'm not sure of the exact syntax. I did some searching online but I have only found examples of insert statement...and I know they have a little different syntax. Thanks
asked Jun 20, 2017 at 1:10
Matthew Tuman
492 silver badges9 bronze badges
1 Answer 1
This should work:
q = "SELECT * FROM courses WHERE course_area = %(value)s "
params = {'value':'some_value_here'}
c.execute(q, params)
.....
answered Jun 20, 2017 at 1:17
gold_cy
14.2k4 gold badges27 silver badges55 bronze badges
Sign up to request clarification or add additional context in comments.
2 Comments
Matthew Tuman
Thanks...I had to remove the single quotes around some_value_here but other than that it works perfectly. I edited my code to reflect the changes.
gold_cy
yea it doesn't have to be a string I simply put quotes around it to indicate it was some random value, and you're welcome
default