数据库与 FMDB 打开连接时出错

发布于 2024-10-05 07:14:45 字数 1783 浏览 8 评论 0原文

当我要打开与数据库的连接时,控制台显示:“打开错误!:14”。 我在项目的资源文件夹中包含了“mybase.sqlite”,并且我正在使用 FMDB 框架。

对于开放连接,我使用以下代码:

 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;
    }

在 AppDelegate 中,我包含以下代码:

- (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]);  }  }

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]);  }  }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

勿挽旧人 2024-10-12 07:15:10

我认为你的开放路径可能不正确。您指定的路径没有意义,就好像您的数据库文件位于根文件夹中一样。

FMDatabase* db = [FMDatabase databaseWithPath:@"/mybase.sqlite"];

上面应该使用此代码作为文件路径,您在问题中已经有了该路径。

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0]; 
NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:@"mybase.sqlite"];
FMDatabase* db = [FMDatabase databaseWithPath:writableDBPath];

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];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文