2010-03-12 43 views
0

我在閱讀說明時遇到了錯誤。有些事情一定出錯了,但我不太確定我該如何解決它。這是我的代碼,實際上我並不清楚這個代碼中究竟發生了什麼。將記錄載入detailview

- (void) hydrateDetailViewData { 
//if detail view is hydrated then do not get it from database 
if(isDetailViewHydrated) return; 

if(detailStmt == nil) { 
    const char *sql = "select snapTitle, snapDesc from Snap where snapID =?"; 
    if(sqlite3_prepare_v2(database, sql, -1, &detailStmt, NULL) != SQLITE_OK) 
     NSAssert1(0, @"Error while creating detail view statement. '%s'", sqlite3_errmsg(database)); 
    NSLog(@"SQLite= %d", sqlite3_step(detailStmt)); 

} 


if(SQLITE_DONE != sqlite3_step(detailStmt)) { 


// NSString *descStr = [[NSString alloc] 
          //initWithString:sqlite3_column_text(detailStmt, 2)]; 
    NSString *descStr = [NSString stringWithUTF8String:(char *)sqlite3_column_text(detailStmt,2)]; 
self.snapDescription = descStr; 

[descStr release]; 
} 
else 
    NSAssert1(0, @"Error getting description of snap2play. '%s'", sqlite3_errmsg(database)); 

    sqlite3_reset(detailStmt); 

    isDetailViewHydrated = YES; //if hydrated, make sure do not get from database again. 
} 
} 
Error message: 
2010-03-12 16:17:14.377 Snap2Play[51282:20b] SQLite= 101 
2010-03-12 16:17:14.378 Snap2Play[51282:20b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** +[NSString stringWithUTF8String:]: NULL cString' 

???

回答

0

您在sql請求中檢索2列,然後嘗試從第3列中獲取數據(因爲列索引是從零開始的);

NSString *descStr = [NSString stringWithUTF8String: 
     (char *)sqlite3_column_text(detailStmt,2)]; // Should be (detailsStmt, 1)? 

檢查sqlite3_step實際返回的值是多少。我認爲將返回值與SQLITE_ROW而不是SQLITE_DONE進行比較會更加正確,因爲您需要確保處理有效的行數據:

if(SQLITE_ROW == sqlite3_step(detailStmt)) { 
... 
+0

不起作用... – summer 2010-03-12 08:44:51