When I'm going to open connect with database, console says: "error opening!: 14". I included "mybase.sqlite" on folder Resources of my project and I'm using the FMDB framework.
For open connect I'm using this code:
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
FMDatabase* db = [FMDatabase databaseWithPath:@"/mybase.sqlite"];
if (![db open]) {
NSLog(@"Não abriu o banco de dados.");
[pool release];
return 0;
}
In AppDelegate, I included this code:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary
*)launchOptions {
// Override point for customization after application launch. HomeViewController *homeVC = [[HomeViewController alloc] init]; navigationController = [[UINavigationController alloc] initWithRootViewController:homeVC]; [self createEditableCopyOfDatabaseIfNeeded]; [window addSubview:navigationController.view];
[window makeKeyAndVisible];
return YES; }
- (void)createEditableCopyOfDatabaseIfNeeded{ BOOL success; NSFileManager
*fileManager = [NSFileManager defaultManager]; NSError *error; NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString
*documentsDirectory = [paths objectAtIndex:0]; NSString
*writableDBPath = [documentsDirectory stringByAppendingPathComponent:@"mybase.sqlite"]; success = [fileManager fileExistsAtPath:writableDBPath]; NSLog(@"Success %d", success); if (success) return; NSString
*defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"mybase.sqlite"]; success = [fileManager copyItemAtPath:defaultDBPath toPath:writableDBPath error:&error]; if (!success) { NSAssert1(0, @"Failed to create writable database file with message '%@'.", [error localizedDescription]); } }
I think your open path is likely incorrect. You are specifying a path that doesn't make sense, as if your DB file was in the root folder.
FMDatabase* db = [FMDatabase databaseWithPath:@"/mybase.sqlite"];
The above should use this code for the file path, which you already have in the question.
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:@"mybase.sqlite"];
FMDatabase* db = [FMDatabase databaseWithPath:writableDBPath];