L'une des raisons peut-être parce que vous devez vous assurer de la base de données est copiée à partir de votre appui répertoire de l'application des fichiers (lecture seule) à la bibliothèque ou documents avant de l'utiliser. Voici une fonction assurerPrepared d'un de mes exemples qui utilise sqlite. Dans ce cas, il s'appelle contacts.db
- (BOOL)ensureDatabasePrepared: (NSError **)error
{
// already prepared
if ((_dbPath != nil) &&
([[NSFileManager defaultManager] fileExistsAtPath:_dbPath]))
{
return YES;
}
// db in main bundle - cant edit. copy to library if !exist
NSString *dbTemplatePath = [[NSBundle mainBundle] pathForResource:@"contacts" ofType:@"db"];
NSLog(@"%@", dbTemplatePath);
NSString *libraryPath = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) lastObject];
_dbPath = [libraryPath stringByAppendingPathComponent:@"contacts.db"];
NSLog(@"dbPath: %@", _dbPath);
// copy db from template to library
if (![[NSFileManager defaultManager] fileExistsAtPath:_dbPath])
{
NSLog(@"db not exists");
NSError *error = nil;
if (![[NSFileManager defaultManager] copyItemAtPath:dbTemplatePath toPath:_dbPath error:&error])
{
return NO;
}
NSLog(@"copied");
}
return YES;
}