2

I am implementing a search function for my database. For some search terms, the input might be null, so I'm using SQLite's ifnull function. Obviously, users might not input the perfect name, so I'm using LIKE to lookup (The other search terms are omitted here). However, it seems that I'm using the wrong syntax.

This is my query:

args = (nameVar)
cur.execute("SELECT * FROM wines WHERE name LIKE IFNULL('%?%',name) COLLATE NOCASE", args)

This returns no results. However, if I instead use the python variables to build the string instead of using? for the parameters, it works perfectly.

cur.execute("SELECT * FROM wines WHERE name LIKE IFNULL('%"+nameVar+"%',name) COLLATE NOCASE")

This, however, is not a viable solution, as nameVar might be null which gives a string builder error. Any help is appreciated thank you!

1 Answer 1

1

You can use SQLite's concatenation operator || to concatenate the value of the ? placeholder:

SELECT * 
FROM wines 
WHERE name 
LIKE IFNULL('%' || ? || '%', name)
Sign up to request clarification or add additional context in comments.

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.