Thomas Joos
Thomas Joos

Reputation: 2451

How to use the SQLITE3 LIKE statement

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

Upvotes: 2

Views: 5947

Answers (2)

amindfv
amindfv

Reputation: 8448

You can actually just say

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

(Note the single-%s)

Upvotes: 1

Thomas Joos
Thomas Joos

Reputation: 2451

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); 

Upvotes: 4

Related Questions