iPhoneSdk, SQLite database search query not work
i want to know how to pass particular word or character in search query to find records.. i am passing in the format like '%%%@%%'.
but it doesn't give me output... this is my code...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);
}
if i pass a same query with word "s" i got 100 of record in sqlite query builder but if m pass through above code it doesn't serach any record
please help meI worked on the code given above and fixed it as follows
-(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);
}
The changed portions can be found between Double *. you would also like to read this
Two steps that I changed were opening the database connection which You must have done elsewhere and second removing the '' from searchName. Do let us know if this resolves the issue.
精彩评论