I'm targeting iOS 5.1 and trying to copy a database from my application files to my Documents folder. I've done this with this same code on apps in the past so I'm a little confuse as to why it isn't working this time.
This is the method I'm using to check if it exists and copy it over if it doesn't. It's from here.
-(void) checkAndCreateDatabase{
// Check if the SQL database has already been saved to the users phone, if not then copy it over
BOOL success;
// Create a FileManager object, we will use this to check the status
// of the database and to copy it over if required
NSFileManager *fileManager = [NSFileManager defaultManager];
// Check if the database has already been created in the users filesystem
success = [fileManager fileExistsAtPath:self.databasePath];
// If the database already exists then return without doing anything
if(success) return;
NSLog(@"Installing db: %@", self.databasePath);
// If not then proceed to copy the database from the application to the users filesystem
// Get the path to the database in the application package
NSString *databasePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:databaseName];
// Copy the database from the package to the users filesystem
[fileManager copyItemAtPath:databasePathFromApp toPath:self.databasePath error:nil];
//[databasePathFromApp release];
//[fileManager release];
}
When I try to query the db I do so like this:
sqlite3 *database1;
// Open the database from the users filessytem
if(sqlite3_open([self.databasePath UTF8String], &database1) == SQLITE_OK) {
// Setup the SQL Statement and compile it for faster access
NSString *sqlStatement = [NSString stringWithFormat: @"update login set is_logged=0"];
sqlite3_stmt *compiledStatement;
if(sqlite3_prepare_v2(database1, [sqlStatement UTF8String], -1, &compiledStatement, NULL) == SQLITE_OK) {
if(SQLITE_DONE != sqlite3_step(compiledStatement)){
NSAssert1(0, @"Error while updating logged. '%s'", sqlite3_errmsg(database1));
NSLog(@"Error setting logged_in to 0: %s", sqlite3_errmsg(database1));
}
else{
NSLog(@"Made Logged_in=0");
}
}
else{
NSLog(@"Prop problem1: %s", sqlite3_errmsg(database1));
NSLog(@"Couldn't prep 1");
}
// Release the compiled statement from memory
sqlite3_finalize(compiledStatement);
}
else{
NSLog(@"Couldn't even open db1");
}
sqlite3_close(database1);
The sqlite3_open()
function returns false in this case with an sqlite3_errmsg
of no such table: login
.
I have a function that is called every second that creates an object that uses this database object. Could it be that the database hasn't been copied in that second and the next call is interrupting the previous copy? That doesn't sound likely.
Any ideas what might be the issue?
Solution I consulted this question:here and as per point number 2 it seems that my database was not in the "Copy Bundle Resources" list. I added it and everything seems fine now.
Thanks for getting me thinking in the right direction guys.