开发者

sql is throwing Exception

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)];

            }
        }
}开发者_StackOverflow

is working correct.

But when i change my query to const char *sqlStatement = "select * from mytable"; OR

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

then it throws an

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


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;
}


I found a forum thread that has similar exception like you and I hope it will help

According to the forum, some rows in your column has NULL data that you have to check against it using something like this (on the forum as well)

char *localityChars = sqlite3_column_text(init_statement, 12);

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜