Skizz
Skizz

Reputation: 1271

App crashes when attempting to make an SQLite connection/statement

I'm having an issue with SQLite where my app crashes at run time while I'm trying to make a connection to an SQLite database and grab some of its contents. Here's my code:

-(IBAction)setInput:(id)sender
{
    NSString *strStoreNumber;
    NSString *strRegNumber;

    strStoreNumber = StoreNumber.text;
    strRegNumber = RegNumber.text;
    lblStoreNumber.text = strStoreNumber;
    lblRegNumber.text = strRegNumber;

    NSString* databasePath = [[NSBundle mainBundle] pathForResource:@"tblStore" ofType:@"sqlite"];

    if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) 
    {
        NSLog(@"Opened sqlite database at %@", databasePath);
        //...stuff
    } 
    else 
    {
        NSLog(@"Failed to open database at %@ with error %s", databasePath, sqlite3_errmsg(database));
        sqlite3_close (database);
    }

    NSString *querystring;

    querystring = [NSString stringWithFormat:@"SELECT strStore FROM tblStore WHERE strStore = %@;", strStoreNumber];  

    const char *sql = [querystring UTF8String];

    NSString *szStore = nil;
    NSString *szReg = nil;

    sqlite3_stmt *statement = nil;
    if (sqlite3_prepare_v2(database, sql, -1, &statement, NULL)!=SQLITE_OK) //queryString = Statement
    {
        NSLog(@"sql problem occured with: %s", sql);
        NSLog(@"%s", sqlite3_errmsg(database));
    }
    else
    {
        // you could handle multiple rows here
        while (sqlite3_step(statement) == SQLITE_ROW) // queryString = statement
        {            
            szStore = [NSString stringWithUTF8String:(char*)sqlite3_column_text(statement, 0)];
            szReg = [NSString stringWithUTF8String:(char*)sqlite3_column_text(statement, 1)];
        } // while        

    }

    sqlite3_finalize(statement);    
    // go on with putting data where you want    
} 

The errors I'm getting in console:

2012-05-07 09:39:33.619 CCoDBTry[962:f803] Opened sqlite database at /Users/*******/Library/Application Support/iPhone Simulator/5.1/Applications/5DB7A218-A0F6-485F-B366-91FD2F9BC062/CCoDBTry.app/tblStore.sqlite
2012-05-07 09:39:33.623 CCoDBTry[962:f803] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** +[NSString stringWithUTF8String:]: NULL cString'
*** First throw call stack:
(0x1593022 0x1724cd6 0x153ba48 0x153b9b9 0x9d4973 0x27c9 0x1594e99 0xd714e 0xd70e6 0x17dade 0x17dfa7 0x17d266 0xfc3c0 0xfc5e6 0xe2dc4 0xd6634 0x147def5 0x1567195 0x14cbff2 0x14ca8da 0x14c9d84 0x14c9c9b 0x147c7d8 0x147c88a 0xd4626 0x1f82 0x1ef5)
terminate called throwing an exception(lldb)

Any help is greatly appreciated!

EDIT: Note the values I'm looking for in the database are of type VARCHAR. I'm not sure if knowing that makes a difference.

Upvotes: 0

Views: 2037

Answers (2)

nirvana74v
nirvana74v

Reputation: 1091

For accessing values that may be null and we are not sure about it I generally prefer checking it before hand

char *sk = (char *)sqlite3_column_text(statement, 0);
if (sk != nil) {
    szStore = [NSString stringWithUTF8String:sk];
}

This will stop the app from crashing.

Upvotes: 1

Jim
Jim

Reputation: 73946

+[NSString stringWithUTF8String:]: NULL cString

It's complaining that you've fed a null value into stringWithUTF8String:. Your code has two calls to this method, each feeding a value in from the query results. Looking at your query, you are only selecting a single column, strStore. I would assume that the line that assigns to szReg is failing because you are trying to retrieve a value that you haven't selected in your query.

Upvotes: 1

Related Questions