Ios 使用Objective C在数据库条目中搜索字符串
我试图搜索用户在搜索栏中输入的文本,以检查该文本是否包含在任何数据库条目中,并希望将结果存储到NSMutableArrayIos 使用Objective C在数据库条目中搜索字符串,ios,objective-c,sqlite,uisearchbar,uisearchbardelegate,Ios,Objective C,Sqlite,Uisearchbar,Uisearchbardelegate,我试图搜索用户在搜索栏中输入的文本,以检查该文本是否包含在任何数据库条目中,并希望将结果存储到NSMutableArray - (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar { [search resignFirstResponder]; [search endEditing:YES]; str = search.text; app = (AppDelegate *)[[UIApplic
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
[search resignFirstResponder];
[search endEditing:YES];
str = search.text;
app = (AppDelegate *)[[UIApplication sharedApplication]delegate];
app.databasePath = [app getDBPath];
const char *dbpath = [app.databasePath UTF8String];
sqlite3_stmt *statement;
if (sqlite3_open(dbpath, &conn) == SQLITE_OK)
{
NSString *strSelectQuery = [NSString stringWithFormat: @"SELECT songname FROM Songs WHERE songname LIKE '%%@%'", str];
const char *query_stmt = [strSelectQuery UTF8String];
if (sqlite3_prepare(conn, query_stmt, -1, &statement, NULL) == SQLITE_OK)
{
while (sqlite3_step(statement) == SQLITE_ROW)
{
[matchList addObject:[[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement, 0)]];
}
sqlite3_finalize(statement);
}
sqlite3_close(conn);
}
NSLog(@"Total Songs : %i", [matchList count]);
}
上面的代码不起作用,因为我忘了写一行,在输入文本并单击UISEarchBar的搜索按钮后,它会重新加载数据 [[u searchresultable reloadData]强>
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
[search resignFirstResponder];
[search endEditing:YES];
str = search.text;
songList = [[NSMutableArray alloc] init];
app = (AppDelegate *)[[UIApplication sharedApplication]delegate];
app.databasePath = [app getDBPath];
const char *dbpath = [app.databasePath UTF8String];
sqlite3_stmt *statement;
if (sqlite3_open(dbpath, &conn) == SQLITE_OK)
{
NSString *strSelectQuery = [NSString stringWithFormat: @"SELECT * FROM Songs WHERE songname LIKE '%%%@%%'", str];
const char *query_stmt = [strSelectQuery UTF8String];
if (sqlite3_prepare(conn, query_stmt, -1, &statement, NULL) == SQLITE_OK)
{
while (sqlite3_step(statement) == SQLITE_ROW)
{
[songList addObject:[[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement, 0)]];
}
sqlite3_finalize(statement);
}
sqlite3_close(conn);
}
[_searchResultTable reloadData];
}
获取dataclasss中的条目,然后从searchbarHey@VNJ搜索,你有什么问题??提到它..问题是SolvedIt对未来的读者很有用,如果您在代码中或单独添加一些注释来解释您正在做什么以及原始解决方案不起作用的原因。还需要注意的是,您已经为自己设置了“SQL注入”黑客。