10
Jan/12
1

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 '%' || ? || '%'