我想知道如何在搜索查询中传递特定的单词或字符来查找记录。
我传递的格式如'%%% @ %%'
但它没有给我输出...
这是我的代码......
searchName = [NSString stringWithFormat:@"'%%%@%%'",[[NSUserDefaults standardUserDefaults] objectForKey:@"key"]];
-(void)getSearchData
{
NSLog(@"search Name %@", searchName);
searchArray = [[NSMutableArray alloc] init];
const char *sql = "select * from features where lower(name) like ? limit 500";
if(sqlite3_prepare_v2(database, sql, -1, &selectstmt, NULL)!= SQLITE_OK)
{
NSAssert1(0,@"error : failed to prepare statement with message '%s'.",sql ite3_errmsg(database));
}
sqlite3_bind_text(selectstmt, 1, [searchName UTF8String], -1, SQLITE_TRANSIENT);
// sqlite3_bind_text(selectstmt, 2, [state UTF8String], -1, SQLITE_TRANSIENT);
while (sqlite3_step(selectstmt) == SQLITE_ROW)
{
NSMutableDictionary *datah = [[NSMutableDictionary alloc] init];
[datah setObject:[NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt,0)] forKey:@"number"];
[datah setObject:[NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt,1)] forKey:@"name"];
[datah setObject:[NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt,2)] forKey:@"type"];
[datah setObject:[NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt,4)] forKey:@"country"];
[datah setObject:[NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt,5)] forKey:@"lat"];
[datah setObject:[NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt,6)] forKey:@"long"];
[datah setObject:[NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt,7)] forKey:@"elevation"];
[datah setObject:[NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt,8)] forKey:@"area"];
[datah setObject:[NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt,3)] forKey:@"state"];
[searchArray addObject:datah];
[datah release];
}
sqlite3_reset(selectstmt);
}
如果我使用单词“s”传递相同的查询,我在sqlite查询构建器中获得了100条记录但是如果m通过上面的代码则不会搜索任何记录
请帮帮我
答案 0 :(得分:1)
我处理了上面给出的代码并修复如下
-(void)getSearchData:(NSString*)searchText
{
**NSString* searchName = [NSString stringWithFormat:@"%%%@%%",searchText];**
NSLog(@"search Name %@", searchName);
sqlite3 *database;
sqlite3_stmt *selectstmt;
NSString* searchQuery = @"SELECT * FROM contacts WHERE lower(name) like ?";
const char *sql = [searchQuery UTF8String];
**if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) {**
if(sqlite3_prepare_v2(database, sql, -1, &selectstmt, NULL) != SQLITE_OK)
{
//NSAssert1(0,@"error : failed to prepare statement with message '%s'.",sql ite3_errmsg(database));
}
sqlite3_bind_text(selectstmt, 1, [searchName UTF8String], -1, SQLITE_STATIC);
while (sqlite3_step(selectstmt) == SQLITE_ROW)
{
NSLog(@"%@",[NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt,0)]);
}
sqlite3_reset(selectstmt);
}
sqlite3_close(database);
}
可以在Double *之间找到更改的部分。您还想阅读this
我改变的两个步骤是打开数据库连接,你必须在其他地方完成,然后从searchName中删除''。如果这样可以解决问题,请告诉我们。