user426795
user426795

Reputation: 11663

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

            }
        }
}

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'

Upvotes: 0

Views: 92

Answers (2)

Tauquir
Tauquir

Reputation: 6913

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

Upvotes: 1

vodkhang
vodkhang

Reputation: 18741

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

Upvotes: 0

Related Questions