sql抛出异常

时间:2010-08-31 08:18:09

标签: iphone objective-c

if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) {
        const char *sqlStatement = "select count(*) from mytable";
        sqlite3_stmt *compiledStatement;
        if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) {

            while(sqlite3_step(compiledStatement) == SQLITE_ROW) {
                NSString *aRecord = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 0)];

            }
        }
}

正在使用correct

但是当我将查询更改为const char *sqlStatement = "select * from mytable";

const char *sqlStatement = "select columnname from mytable"; 

然后抛出一个

uncaught exception 'NSInvalidArgumentException', reason: '*** +[NSString stringWithUTF8String:]: NULL cString'

2 个答案:

答案 0 :(得分:1)

while(sqlite3_step(compiledStatement) == SQLITE_ROW) {
        char *myChar = (char *)sqlite3_column_text(compiledStatement, 0);
            if (myChar !=NULL) {
            NSString *aRecord = [NSString stringWithUTF8String: myChar];
            }
            else 
                myChar = nil;
}

答案 1 :(得分:0)

我发现forum thread有类似的异常,我希望它会有所帮助

根据论坛,你的列中的某些行有NULL数据,你必须使用类似的东西检查它(在论坛上)

char *localityChars = sqlite3_column_text(init_statement, 12);

if (localityChars ==null)
     self.Locality = nil;
else
     self.Locality = [NSString stringWithUTF8String: localityChars];