2

I am testing DBAccess in order to see if I can use it in a project. I have problems changing the location of the sqlite file, I want it to be in cache directory. Here is part of the code I wrote until now, in AppDelegate.m :

- (DBAccessSettings*)getCustomSettings {
    DBAccessSettings *settings = [[DBAccessSettings alloc] init];
    NSURL *applicationCachesDirectory = [[[NSFileManager defaultManager] URLsForDirectory:NSCachesDirectory inDomains:NSUserDomainMask] lastObject];
    settings.databaseLocation = [applicationCachesDirectory absoluteString];
    settings.defaultDatabaseName = @"myDefaultDB";
    return settings;
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    [DBAccess setDelegate:self];
    [DBAccess openDatabaseNamed:@"NSURLProtocolExample"];    
    [NSURLProtocol registerClass:[MyURLProtocol class]];
    return YES;
}

But with the above code I get the following error message:

error >> unable to open database file

Can you shed some light in here?

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
Horatiu
  • 151
  • 2
  • 12

1 Answers1

1

I'm not familiar with DBAccess, but you have these lines:

NSURL *applicationCachesDirectory = [[[NSFileManager defaultManager] URLsForDirectory:NSCachesDirectory inDomains:NSUserDomainMask] lastObject];
settings.databaseLocation = [applicationCachesDirectory absoluteString];

That will retrieve a URL string (one that starts with file://), not a file path. If you want the path of that folder, you want to use path:

settings.databaseLocation = [applicationCachesDirectory path];

Frankly, the documentation is ambiguous as to whether its expecting a URL string or a path, but I would assume it expecting the latter.

Rob
  • 415,655
  • 72
  • 787
  • 1,044
  • 1
    Thank you Rob for pointing out the floor in the documentation, I have amended the comment to be more specific (along with lots of the other parts of it) and this will be included in the next build/release – Adrian_H Jan 27 '15 at 15:02