将记录加载到detailview中

时间:2010-03-12 06:40:18

标签: iphone sqlite

我在阅读说明时遇到了错误。有些东西肯定出错了,但我不太确定我该怎么解决它。这是我的代码,实际上我并不知道这段代码到底发生了什么。

- (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'

???

1 个答案:

答案 0 :(得分: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)) {
...