tags:

views:

471

answers:

1
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'
A: 

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

Doug Currie
I changed the code like below but it throws another exception.(I filled attributes in my table with text. Nothing is nil) char *rowdata = (char *)sqlite3_column_text(compiledStatement, 1); araci = [[NSString alloc] stringWithUTF8String:rowdata];this is the exception -[NSPlaceholderString stringWithUTF8String:]: unrecognized selector sent to instance 0x3d0c0c0
Perhaps: araci = [[NSString alloc] initWithUTF8String:rowdata];
Doug Currie
ahmet732: `stringWithUTF8String:` is a class method. `alloc` is also a class method, and returns an instance. Therefore, you sent the `stringWithUTF8String:` message to an instance, not to the NSString class. (And you sent that message to an *uninitialized* instance, since you never sent it an `init` or `initWith…:` message.) Use `[NSString stringWithUTF8String:]`, as you did in the code in your question, or `alloc` and `initWithUTF8String:`, as Doug Currie suggested.
Peter Hosey
ahmet723: Worth noting that the ownership policies for those two methods differ. `alloc` returns an object that you must own, whereas `stringWithUTF8String:` returns an object that you do not own. See http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/MemoryMgmt/ for details.
Peter Hosey