Profo
Profo

Reputation: 153

Can't find the database file

I can't figure why db.sqlite can't be found. I added it to the resource dir of the project. It is there. Here is some code how I check for existing.

-(void)getDatabaseLocation
{
    NSArray *dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

    NSString *docsDir = [dirPaths objectAtIndex:0];

    // Build the path to the database file
   NSString *databasePath = [[NSString alloc] initWithString: [docsDir stringByAppendingPathComponent: @"db.sqlite"]];

   NSFileManager *filemgr = [NSFileManager defaultManager];
     if ([filemgr fileExistsAtPath: databasePath ] == NO)
     {
         NSLog(@"NO"); // It's say no when this method  is called.
     }
    else
    {   NSLog(@"YES");
    }
}

//EDIT Now I check that the path in docsDir doesn't exist.

Upvotes: 0

Views: 296

Answers (1)

Richard J. Ross III
Richard J. Ross III

Reputation: 55583

Of course it isn't there. You must first copy the resource to the documents directory, like this:

if (![filemgr fileExistsAtPath:databasePath])
{
    [filemgr copyItemAtPath:[NSBundle.mainBundle pathForResource:@"db" ofType:@"sqlite"] toPath:databasePath error:nil]; 
}

EDIT: If you only need to do lookups on the database (no editing), then you can simply do this:

databasePath = [NSBundle.mainBundle pathForResource:@"db" ofType:@"sqlite"];

Upvotes: 2

Related Questions