开发者

Error for open connect of database with FMDB

开发者 https://www.devze.com 2023-01-28 09:42 出处:网络
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.

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:@"m开发者_C百科ybase.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];
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号