2

I am trying to retrieve data from sql through python with the code:

query = ("SELECT stuff FROM TABLE WHERE name like %%(this_name)s%")
result = pd.read_sql(query,con=cnx,params={'this_name':some_name})

The code above works perfectly when I don't have to pass the wildcard operator %. However, in this case the code doesn't work. How can I pass in the query the wildcard operator? Thank you.

1

2 Answers 2

3

Consider concatenating the wildcard operator, %, to passed in value:

query = ("SELECT stuff FROM TABLE WHERE name LIKE %(this_name)s") 
result = pd.read_sql(query,con=cnx, params={'this_name': '%'+ some_name +'%'})
Sign up to request clarification or add additional context in comments.

Comments

0

Try using a docstring and some quotes around the like pattern. It appears you are using the pyformat paramstyle.

query = """SELECT stuff FROM TABLE WHERE name LIKE '%%(this_name)s%'"""
results = pd.read_sql(query, con=cnx, params={'this_name': some_name})

Comments

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.