2011-06-21 78 views
0

在我的應用程序中,我正在執行一個查詢以獲取具有相同類別的圖像數量,並且該類別作爲參數傳遞給我。現在,當執行查詢時,我想得到兩個結果,不是1就是多個。爲此,我想我需要將字符串轉換爲整數。當我檢查字符串的值時,它會顯示1,但是當該值返回給該方法時,顯示如下所示:695842以及其他內容。可能是什麼原因?請幫忙。我張貼的代碼部分: -Sqlite數據庫中的查詢後返回值的問題

-(NSInteger)searchImagesInSameCategory:(NSString *)categoryName 
{ 

    NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 

    NSString *documentsDirectory= [paths objectAtIndex:0]; 

    NSString *path=[documentsDirectory stringByAppendingPathComponent:@"SymbolTalkLanguageElement.sqlite"]; 


    //Open the database 
    //might have to make database as property 
    if(sqlite3_open([path UTF8String], &dataBase) ==SQLITE_OK) 
    { 



     sqlite3_stmt *statement; 

     NSString *strSQL = [[NSString alloc]init]; 


     strSQL = @"select Count(ImageName) from tblLanguageElement where Category='"; 

     strSQL = [[strSQL stringByAppendingString:categoryName] stringByAppendingString:@"'"]; 


     const char *bar = [strSQL UTF8String]; 


     if(sqlite3_prepare(dataBase, bar, -1, &statement, NULL) == SQLITE_OK) 
     { 

      while (sqlite3_step(statement) == SQLITE_ROW) 
      { 

       NSLog(@"%@",[NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 0)]); 

       int k; 

       //NSString *string=[NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 0)]; 

       //NSLog(string); 

       k=[NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 0)]; 

       //NSLog([NSString stringWithFormat:@"Count for k:-%d",k]); 

       //[list addObject:[NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 0)]]; 

       //return [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 0)]; 

       return k; 
      } 

     } 



    } 
    return 1; 

} 

回答

1

您應該使用intValue方法來轉換NSStringint

NSString *integerStr = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 0)]; 
k = [integerStr intValue]; 
+0

非常感謝......在不久的幾天 – Christina

+0

@Christina,乾杯:-)預期 – EmptyStack

+0

其它更多的幫助......。堆棧溢出很樂意幫助每一個:-) – Christina