2010-03-01 43 views
0

我需要一些幫助!請。我正在嘗試開發一個非常簡單的應用程序,基本上只是一個簡單的圖書閱讀器。理想情況下,第一個視圖將是一個列出章節名稱的可用視圖,然後選擇該行將帶您進入詳細視圖,該視圖將該章節分解爲多個段。注意這些段實際上是它們自己在表格內的記錄。所以我試圖用db表中的多個記錄來填充uitext視圖。從這個數據庫中獲取所有數據的過程就是我所有搞砸了的地方。我在網上搜索了一些教程,我發現了一個非常有用的教程here。我一步一步地經歷了它,這是有道理的,這一切都是有道理的。但不是使用他提供的數據庫,我想嘗試插入我自己的數據庫,它的所有內容Id都喜歡可見,並且它不會工作。它扔了一個不錯的*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** +[NSString stringWithUTF8String:]: NULL cString'錯誤,我不明白。這聽起來對任何人都熟悉或有意義嗎?對於MTHE readFromDB方法的代碼是:從sqlite db導入多條記錄iphone sdk

-(void) readTextFromDatabase { 
// Setup the database object 
sqlite3 *database; 

// Init the animals Array 
textAr = [[NSMutableArray alloc] init]; 

// Open the database from the users filessytem 
if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) { 
    // Setup the SQL Statement and compile it for faster access 
    const char *sqlStatement = "select * from books";//This is where I'm running into trouble 
    sqlite3_stmt *compiledStatement; 
    if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) { 
     // Loop through the results and add them to the feeds array 
     while(sqlite3_step(compiledStatement) == SQLITE_ROW) { 
      // Read the data from the result row 
      NSString *aChapter = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 1)]; 
      NSString *aSection = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 2)]; 


      // Create a new text object with the data from the database 
      Text *text = [[Text alloc] initWithChapter:aChapter Section:aSection]; 

      // Add the text object to the text Array 
      [textAr addObject:text]; 

      [text release]; 
     } 
    } 
    // Release the compiled statement from memory 
    sqlite3_finalize(compiledStatement); 

} 
sqlite3_close(database); 

除了交換一些變量的名字,我沒有改變代碼的任何部分,除了我提到的可能導致一些問題的地方。我將「select * from animals」語句更改爲「select * from books」,其中animals是舊錶的名稱,books是我希望使用的新db中表的名稱。我認爲這不會太大,但顯然是這樣。我認識到這不是一個非常整潔的問題,但如果有人能夠幫助我穿過這面牆,那麼我將不勝感激。謝謝!!!

回答

1

我在使用sqlite時遇到了同樣的問題,我無法準確記得空字符串何時出現(可能來自行中的NULL值),但我使用此代碼來解決問題:

+ (NSString*)stringWithCharsIfNotNull: (char*)chars 
{ 
    if (chars == NULL) 
     return nil; 
    else 
     return [[NSString stringWithUTF8String: chars] 
       stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]]; 
}