views:

1549

answers:

2

I am making an app with many database operations. Since SQLite caches data, in my applicationDidReceiveMemoryWarning method, I am closing the database and opening it again to delete the cached data.

When the do this, I get this error *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Error: failed to open database with message 'not an error'.'

Here's the code I am using to close the database and open again where database is of type sqlite3* [MySQLInter finalizeStatements];

 if(sqlite3_close(database) == SQLITE_OK){

//open again
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

NSString *documentsDirectory = [paths objectAtIndex:0];

NSString *path = [documentsDirectory stringByAppendingPathComponent:@"mydb.sql"];


    if(sqlite3_open([path UTF8String], &database) != SQLITE_OK){

         NSAssert1(0, @"Error: failed to open database with message '%s'.", sqlite3_errmsg(database));

}

 }

 else{

NSAssert1(0, @"Error: failed to close database on memwarning with message '%s'.", sqlite3_errmsg(database));

 }

Can someone please tell me how to avoid this or what I am doing wrong?

Thanks

+1  A: 

Are you sure you don't mean "!= SQLITE_OK" on the sqlite3_open call?

Marco
A: 

sqlite3_open returns an error code. You're just checking it for SQLITE_OK - it'd be interesting to see exactly what it's returning. If it doesn't open, it's not valid to call sqlite3_errmsg on database because it didn't open the database, so that object may not be populated properly.

In your error catching conditional, print out the value of the return of sqlite3_open instead, and we'll be able to help better.

Dan Keen