如何使用SQLITE3 LIKE语句

时间:2010-04-15 08:27:01

标签: iphone sqlite statements

我在项目中运行动态LIKE语句时遇到问题: 此查询的工作方式类似于魅力,并返回名称中带有“t”的所有项目:

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

当我尝试动态地执行此操作时,我不会收到错误,但只是一个空的结果。似乎该值为null。我尝试绑定一个输出正确值的字符串值's'

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

我应该如何绑定此值而不是使用:

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

2 个答案:

答案 0 :(得分:4)

刚刚在http://www.innerexception.com/2008/10/using-like-statement-in-sqlite-3-from.html

找到了很好的解释

我改变了以下几行:

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

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

答案 1 :(得分:1)

你实际上可以说

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

(注意单 - % s)