1

データベースとの接続を開くと、コンソールに「エラーが発生しました!: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]);  }  }
4

1 に答える 1

13

あなたの開いているパスはおそらく間違っていると思います。DB ファイルがルート フォルダーにあるかのように、意味のないパスを指定しています。

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];
于 2010-12-02T21:59:04.703 に答える