10
Jan/121
Jan/121
FMDB like condition with bind parameters
I’m working on an iOS project that uses the FMDB SQLite wrapper. I was trying to write a query with like condition and bind parameter, and it wasn’t returning any results. For example:
SELECT * FROM stuff WHERE name LIKE '%?%'
It turns out that this is because %?% is being treated as a string literal since it’s inside single quotes. To workaround this, you just need to build the condition using the ANSI SQL concatenation operator (||), just like if you were concatenating a String. For example:
SELECT * FROM stuff WHERE name LIKE '%' || ? || '%'
January 11th, 2012
I’ve had this exact problem in a good number of other languages too – PHP and even Coldfusion with cfparam. Concat is a good fix – what I think I usually end up doing is just building the variable with “%”‘s inside of it and then just binding that variable.