我已经完成了根据给定的主要类别ID提取子类别的方法。
运行此方法时,它只会获取一条记录。我的意思是控制来
while(sqlite3_step(selectstmt) == SQLITE_ROW)
while
仅循环一次。当在SQL浏览器中执行相同的查询时,它可以正常工作并按预期获取结果。我不知道为什么在运行该函数时这不起作用
请让我知道,问题出在下面的代码上
- ( NSMutableDictionary * ) getDataToDisplayTierTwo:(NSString*)dbPath:(NSString*)iD{
NSMutableDictionary *aTierTwoData = [[NSMutableDictionary alloc]init];
if (sqlite3_open([dbPath UTF8String], &database) == SQLITE_OK)
{
NSString *selectSQL = [NSString stringWithFormat: @"select * from sub_categories_reference scr inner join sub_categories ssc on ssc.id = scr.sub_category_id where scr.main_category_id = %@ ",iD];
const char *sql_query_stmt = [selectSQL UTF8String];
sqlite3_stmt *selectstmt;
if(sqlite3_prepare_v2(database, sql_query_stmt, -1, &selectstmt, NULL) == SQLITE_OK)
{
while(sqlite3_step(selectstmt) == SQLITE_ROW)
{
NSLog(@"coming insode");
NSString *aValue = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(selectstmt, 6)];
NSString *aId = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(selectstmt, 5)];
[aTierTwoData setObject:aId forKey:aValue];
[aValue release];
[aId release];
NSLog(@"%@ %@ ^^^^^^^^picker value id ", aValue, aId);
}
}
}
else{
//Even though the open call failed, close the database connection to
sqlite3_close(database);
}
return aTierTwoData;
}
最佳答案
- ( NSMutableArray * ) getDataToDisplayTierTwo:(NSString*)dbPath:(NSString*)iD{
NSMutableDictionary *aTierTwoData = [[NSMutableDictionary alloc]init];
NSMutableArray *aTierArray = [[NSMutableArray alloc]init];
if (sqlite3_open([dbPath UTF8String], &database) == SQLITE_OK)
{
NSString *selectSQL = [NSString stringWithFormat: @"select * from sub_categories_reference scr inner join sub_categories ssc on ssc.id = scr.sub_category_id where scr.main_category_id = %@ ",iD];
const char *sql_query_stmt = [selectSQL UTF8String];
sqlite3_stmt *selectstmt;
if(sqlite3_prepare_v2(database, sql_query_stmt, -1, &selectstmt, NULL) == SQLITE_OK)
{
while(sqlite3_step(selectstmt) == SQLITE_ROW)
{
NSLog(@"coming insode");
NSString *aValue = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(selectstmt, 6)];
NSString *aId = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(selectstmt, 5)];
NSLog(@"%@ %@ ^^^^^^^^picker value id ", aValue, aId);
[aTierTwoData setObject:aId forKey:aValue];
[aValue release];
[aId release];
[aTierArray addObject:aTierTwoData];
aTierTwoData = nil;
}
}
}
else{
//Even though the open call failed, close the database connection to
sqlite3_close(database);
}
return aTierArray;
}
关于iphone - sqlite只返回一条记录,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/8830197/