【问题标题】:Error for open connect of database with FMDB使用 FMDB 打开连接数据库时出错
【发布时间】:2011-05-19 08:07:41
【问题描述】:

当我要打开与数据库的连接时,控制台说:“错误打开!: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]);  }  }

【问题讨论】:

  • 卢卡斯,您应该将答案标记为已接受,因为它回答了您的问题。
  • @LucasMoreria 这还没有被接受?这是一个完美的答案。
  • @slycrel 的答案是完美的,它正在工作。您应该将答案标记为已接受。
  • 我的错!我是新来的。对此感到抱歉。他的回答确实很棒。

标签: objective-c cocoa sqlite touch fmdb


【解决方案1】:

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

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

【讨论】:

  • 可行,但第一行不正确。它的工作原理是这样的: NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  • doh,我从原始描述中复制粘贴错误。谢谢!
猜你喜欢
  • 1970-01-01
  • 2015-07-23
  • 1970-01-01
  • 1970-01-01
  • 2018-09-08
  • 2013-01-03
  • 2011-04-22
  • 1970-01-01
  • 2012-05-15
相关资源
最近更新 更多