Hi,
I am having issues with closing and reopening my sqlite3 database within my app.
Basically on application did receive memory warning I am closing and reopening the database. I don't get any errors at this point but then the next time I try to access the database I get the following error...
Does anyone have any idea what I might be doing wrong here?
I am having issues with closing and reopening my sqlite3 database within my app.
Basically on application did receive memory warning I am closing and reopening the database. I don't get any errors at this point but then the next time I try to access the database I get the following error...
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Error: failed to prepare statement with message 'library routine called out of sequence'.'
Code:
- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application {
if ([self closeDatabase]){
[self openDatabase];
}
}
Code:
- (BOOL)closeDatabase{
// Close the database.
if (sqlite3_close(_database) != SQLITE_OK) {
NSLog(@"Error: failed to close database with message '%s'.", sqlite3_errmsg(_database));
return FALSE;
}
databaseIsOpen = FALSE;
return TRUE;
}
Code:
- (BOOL)openDatabase{
if (databaseIsOpen){
return databaseIsOpen;
}
// Open the database.
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"db.sql"];
// Open the database. The database was prepared outside the application.
if (sqlite3_open([path UTF8String], &_database) != SQLITE_OK) {
NSLog(@"Error: failed to open database with message '%s'.", sqlite3_errmsg(_database));
return FALSE;
}
databaseIsOpen = TRUE;
return databaseIsOpen;
}
Does anyone have any idea what I might be doing wrong here?