sqlite只返回一条记录

时间:2012-01-12 04:37:40

标签: iphone ios sqlite ios4

我已经完成了根据给定的主要类别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;        
}

1 个答案:

答案 0 :(得分:1)

- ( 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;        
}