2011-05-03 44 views
0

HII每一個問題,同時存儲在數據庫到表視圖加載數據

我使用下面的代碼保存在數據庫到表視圖加載數據,但其crasing當我重新啓動應用程序

InsertRecord是類insertUpdateDelete的

+ (void) getInitialDataToDisplay:(NSString *)dbPath 
{ 


    iICS_testAppDelegate *appDelegate = (iICS_testAppDelegate *)[[UIApplication sharedApplication] delegate]; 

    if (sqlite3_open([dbPath UTF8String], &database) == SQLITE_OK) { 

     const char *sql = "select * from tbl_Users"; 
     sqlite3_stmt *selectstmt; 
     if(sqlite3_prepare_v2(database, sql, -1, &selectstmt, NULL) == SQLITE_OK) { 

      while(sqlite3_step(selectstmt) == SQLITE_ROW) { 

       NSInteger primaryKey = sqlite3_column_int(selectstmt, 0); 
       insertUpdateDelete *InsertRecord = [[insertUpdateDelete alloc] initWithPrimaryKey:primaryKey]; 

       InsertRecord.strFirstName = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 1)]; 
       InsertRecord.strMiddleName = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 2)]; 
       [appDelegate.arrObjects addObject:InsertRecord]; 
       [InsertRecord release]; 
      } 
     } 
    } 
    else 
    { 
     sqlite3_close(database); //Even though the open call failed, close the database connection to release all the memory. 
    } 
    NSLog(@"arrObjects----%@",appDelegate.arrObjects); 
} 

應用在下面的行

InsertRecord.strFirstName = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 1)]; 
崩潰實例3210

崩潰日誌is--終止應用程序由於未捕獲的異常 'NSInvalidArgumentException',原因是: '* + [NSString的stringWithUTF8String:]:NULL CSTRING'

回答

1

您需要檢查數據庫中的空數據;

// load char in temporary variable 
char *tempChar = sqlite3_column_text(selectstmt, 1); 

if(tempChar == null) // test for null 
    InsertRecord.strFirstName = nil; 
else 
    InsertRecord.strFirstName = [NSString stringWithUTF8String:tempChar]; 

// go to the next field in the database 
tempChar = sqlite3_column_text(selectstmt, 2); 

// do the same type of test 
0

測試如果sqlite3_column_text(selectstmt, 1)返回一個有效的字符串。

0
(char *)sqlite3_column_text(selectstmt, 1) should be given nil (null) value. 

所以,當你從數據庫中獲取的數據添加校驗。

一樣,

if ((char *)sqlite3_column_text(selectstmt, 1) != nil) 
{ 
    InsertRecord.strFirstName = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 1)]; 

} 
else 
{ 
    InsertRecord.strFirstName = @""; 
} 
相關問題