2012-01-12 159 views
0

我已經完成了根據給定的主類別id獲取子類別的方法。sqlite只返回一條記錄

運行此方法時,它只取一條記錄。我的意思是控制來到

while(sqlite3_step(selectstmt) == SQLITE_ROW) 

while只循環一次。

當執行相同的查詢到SQL瀏覽器能正常工作並獲取結果expected.I不知道爲什麼這個運行功能不工作時

請讓我知道,有什麼問題就出在

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

感謝您的答覆下面的代碼...什麼是我的代碼 – user198725878 2012-01-12 05:58:05

+0

的問題是它的工作?或不? – Anand 2012-01-12 06:28:01

+0

它不起作用。這也是隻返回一條記錄 – user198725878 2012-01-12 07:01:35