3

我以以下格式存储数据

             05/03/2012 10:11PM : <0818aaaa aaaaaaaa aaaa454d 554c4154 4f520000 00000000 0000>

但是当检索数据时,我使用以下方法

    sqlite3 *database;

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

    // Setup the SQL Statement and compile it for faster access
    const char *sqlStatement = "select logText from logTable";

    sqlite3_stmt *compiledStatement;


    if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) {



        while(sqlite3_step(compiledStatement) == SQLITE_ROW) {              

            NSString *addressField = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(compiledStatement, 0)];

            // Read the data from the result row

            fullLog=[NSString stringWithFormat:@"%@%@",fullLog,addressField];

        } // end of the while

    }
    sqlite3_finalize(compiledStatement);
}
sqlite3_close(database);

我收到以下错误,

    Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSPlaceholderString initWithUTF8String:]: NULL cString'

*在第一次抛出时调用堆栈:

请帮助为什么会出现这个问题

4

2 回答 2

4

您不能NSString使用指针初始化对象,NULL因此只需检查该条件:

const char* addressField_tmp = (const char*)sqlite3_column_text(compiledStatement, 0);

NSString *addressField = addressField_tmp == NULL ? nil : [[NSString alloc]           initWithUTF8String:addressField_tmp];
于 2012-10-31T04:25:32.917 回答
0

请使用以下代码修复它,而不是您的代码。

  NSString *addressField = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 0)];
于 2012-05-03T18:34:39.257 回答