2

Hi I was looking for a similar thread but couldn't find any so decided to post. Any hint or link would be appreciated.

I'm using python 2.7 + MySQLdb, I made a sql statement but having a weird issue.

【works】

Val3 = 1
sql = """SELECT ValA FROM %s WHERE Val2 = %s AND Val3 = %s""" % (Val1, Val2, Val3)

【doesn't work】

Val3 = "variable"
sql = """SELECT ValA FROM %s WHERE Val2 = %s AND Val3 = %s""" % (Val1, Val2, Val3)

When I use text as a variable sql statement doesn't get executed.

(Note: no error message just gets treated like as if its not there)

Does anyone know what the problem is?

1 Answer 1

11

Add quotes:

sql = """SELECT ValA FROM `%s` WHERE Val2 = '%s' AND Val3 = '%s'""" % (Val1, Val2, Val3)
Sign up to request clarification or add additional context in comments.

1 Comment

do not forget the escape quotes.

Your Answer

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.