I'm doing a very classic routine that i've done multiple times, but in this project, its not working. When I want to work with my DB, I get " No such tables " errors. Which should be wrong. I checked the bundle database and its fine ; I checked the "result" db in the phone and its completly empty (no structure, and obviously no data).
Here is my DB creation routine. I call it every time I need the database.
+ (FMDatabase*)createAndCheckDataBase{
BOOL success;
NSArray *docPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [docPaths objectAtIndex:0];
NSString *databasePath = [documentsDir stringByAppendingPathComponent:@"database.sqlite"];
NSFileManager *fileManager = [NSFileManager defaultManager];
success = [fileManager fileExistsAtPath:databasePath];
//The database already exist in the application folder, we don't need to create it
if(success){
return [FMDatabase databaseWithPath:databasePath];
}
NSString *databasePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"database.sqlite"];
[fileManager copyItemAtPath:databasePathFromApp toPath:databasePath error:nil];
return [FMDatabase databaseWithPath:databasePath];
Then i just do FMDatabase *db = [DatabaseManager createAndCheckDatabase];
and i supposedly get my db. But i don't.
I created the sqlite database using the SQLite Manager plugin from Firefox, created the structure there, then imported it into the bundle.
Note : success always return true.
Any help is most welcome!
As the comments mentioned, you need to check the copy.
Here's a snippet from my code which checks whether the copy succeeded and returns the error details to the consumer
//
// notice our method to prepare the db returns bool and doesn't lose the error details
// the consumer can log, present, whatever - this is a library function
//
- (BOOL)ensureDatabasePrepared: (NSError **)error
{
// <snip bunch of code>
// copy db from template to library
if (![[NSFileManager defaultManager] fileExistsAtPath:_dbPath])
{
NSLog(@"db not exists");
// notice how we pass in the error ref passed to this function
if (![[NSFileManager defaultManager] copyItemAtPath:dbTemplatePath toPath:_dbPath error:error])
{
return NO;
}
NSLog(@"copied");
}