开发者

Having problems binding data using % wildcards

Using sqlite and building queries, they all work fine until I try to bind certain data into a query such as -

NSString *query = @"SELECT id, title FROM fields WHERE (staff LIKE '%?%');
sqlite3_prepare_v2(lessonPlansDatabase, [query UTF8String], -1, &stmt, nil);

NSString *theValue = @"Fred";
sqlite3_bind_text(stmt, 1, [theValue UTF8String], -1, NULL);

The problem appears to be with using the '%'. I've tried putting it into the string which I'm binding, but that doesn't work either. If I just make the query in full, like -

NSString *query = @"SELECT id, title FROM fields WHERE (staff LIKE '%Fred%');

and don't bind anything, it works fine. But I don't want to do this in case there are characters in the string which upset the query, I'd prefer to let sqlite do the binding. The binding works fine where there are no '%' cha开发者_开发问答racters.

Any help much appreciated!


You want

NSString *query = @"SELECT id, title FROM fields WHERE (staff LIKE ?)";
sqlite3_prepare_v2(lessonPlansDatabase, [query UTF8String], -1, &stmt, nil);

NSString *theValue = @"%Fred%";
sqlite3_bind_text(stmt, 1, [theValue UTF8String], -1, NULL);


Escape the (%)s with (%) and try to bind text.

(staff LIKE '%%?%%')
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜