Question

I have problems running a dynamic LIKE statement in my project: this query works like a charm and returns all items with a 't' in their name:

const char *sql = "select * from bbc_ipad_v1_node where name LIKE '%%t%%'";

When I try to do this dynamically I do not get errors, but just an empty result. It seems that the value is null. I try to bind a string value 's' which outputs a correct value

NSLog(@"bbc_ : search menu items from db based on: %@",s);
const char *sql = "select * from bbc_ipad_v1_node where name LIKE '%%?%%'";
sqlite3_stmt *statement;
if (sqlite3_prepare_v2(database, sql, -1, &statement, NULL) == SQLITE_OK) {
sqlite3_bind_text(statement, 1, [s UTF8String],-1,SQLITE_TRANSIENT);

How should I bind this value instead of using:

const char *sql = "select * from bbc_ipad_v1_node where name LIKE '%%?%%'";
Was it helpful?

Solution

Just found a great explanation at http://www.innerexception.com/2008/10/using-like-statement-in-sqlite-3-from.html

I changed following lines:

const char *sql = "select * from bbc_ipad_v1_node where name LIKE ?001";

and

NSString *searchInput = [NSString stringWithFormat:@"%@%%", s];
sqlite3_bind_text(statement, 1, [searchInput UTF8String],-1,SQLITE_TRANSIENT); 

OTHER TIPS

You can actually just say

const char *sql = "select * from bbc_ipad_v1_node where name LIKE '%t%'";

(Note the single-%s)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top