0

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

1 Answer 1

0

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
Sign up to request clarification or add additional context in comments.

2 Comments

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.
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

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.