Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what's wrong with sql statements sqlite3_column_text?

Tags:

sqlite

cocoa

if(sqlite3_open([databasePath UTF8String], & database) == SQLITE_OK) {
    NSLog(@"DB OPENED");
    // Setup the SQL Statement and compile it for faster access
    const char *sqlStatement ="select name from Medicine";

    sqlite3_stmt *compiledStatement;

    int result = sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, nil); 
    NSLog(@"RESULT %d", result);

    if(result == SQLITE_OK) {
        NSLog(@"RESULT IS OK...");

        // Loop through the results and add them to the feeds array
        while(sqlite3_step(compiledStatement) == SQLITE_ROW) {
            // Read the data from the result row
            NSLog(@"WHILE IS OK");

            **araci = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 1)];**

            NSLog(@"Data read");


            NSLog(@"wow: %",araci);


        }//while 
    }//if sqlite_prepare v2
    // Release the compiled statement from memory
    sqlite3_finalize(compiledStatement);
    NSLog(@"compiled stmnt finalized..");
}
sqlite3_close(database);
NSLog(@"MA_DB CLOSED");

Questions

1 ) Everything is alright until bold code. when i run the app, throws an exception below. I have one table and one row in my db relatively, id (integer), name (varchar), desc (varchar) (I created my table using SQLite Manager).

2-) What is text type in SQLite Manager? Is that NSString? Also how can I put the string values(in table) to an NSMutableArray ? (I am really having difficulty in conversion.)

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** +[NSString stringWithUTF8String:]: NULL cString'
like image 795
user310000 Avatar asked Aug 13 '26 17:08

user310000


2 Answers

to handle null returned by the database, you can use the following code to tackle it


char *localityChars =  (char *)sqlite3_column_text(init_statement, 12);

if (localityChars ==null)
     self.Locality = nil;
else
     self.Locality = [NSString stringWithUTF8String: localityChars];
like image 193
yunas Avatar answered Aug 16 '26 06:08

yunas


sqlite3_column_text will return a null pointer when the column is NULL. You need to check for this.

like image 22
Doug Currie Avatar answered Aug 16 '26 07:08

Doug Currie



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!