1

データベースがまだ作成されていない場合にデータベースを追加するコードがあります。これがコードです

-(void) checkAndCreateDatabase{
  // Check if the SQL database has already been saved to the users phone, if not then copy it over

  NSString *path=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];

  NSString *database_path=[path stringByAppendingPathComponent:@"Favorite_Database.sqlite"];

  // Create a FileManager object, we will use this to check the status
  // of the database and to copy it over if required

  // Check if the database has already been created in the users filesystem
  BOOL success = [[NSFileManager defaultManager] fileExistsAtPath:database_path];

  // If the database already exists then return without doing anything
  if(success) {
      //[fileManager removeFileAtPath:databasePath handler:nil];   
      return;
  }
  // If not then proceed to copy the database from the application to the users filesystem

  // Get the path to the database in the application package
  NSString *databasePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"Favorite_Database.sqlite"];

  // Copy the database from the package to the users filesystem
  [[NSFileManager defaultManager] copyItemAtPath:databasePathFromApp toPath:database_path error:nil];

}

それがラインに当たるBOOL success = [[NSFileManager defaultManager] fileExistsAtPath:database_path];と、それはスキップし、残りは続行します。ifステートメントに到達しません。このデータベースを追加できるように確認する必要があります

どうすれば正しく動作させることができますか?

4

1 に答える 1

0

エミュレーターで実行している場合は、ターミナルでそのパスに移動して、ファイルが実際に存在するかどうかを確認できるように、パスをログアウトします。sqlite コマンドラインを使用して db ファイルを検査することもできます。

同様のタスクを実行する私のサンプルのコードを次に示します。

(1) データベースが存在するかどうかを確認し、存在しない場合は、(2) バンドルからコピーし、(3) 開きます。

それが役に立てば幸い ...

- (BOOL)ensureDatabaseOpen: (NSError **)error
{
    // already created db connection
    if (_contactDb != nil)
    {
        return YES;
    }

    NSLog(@">> ContactManager::ensureDatabaseOpen");    
    if (![self ensureDatabasePrepared:error])
    {
        return NO;
    }

    const char *dbpath = [_dbPath UTF8String]; 
    if (sqlite3_open(dbpath, &_contactDb) != SQLITE_OK &&
        error != nil)
    {
        *error = [[[NSError alloc] initWithDomain:@"ContactsManager" code:1000 userInfo:nil] autorelease];
        return NO;
    }

    NSLog(@"opened");

    return YES;
}

- (BOOL)ensureDatabasePrepared: (NSError **)error
{
    // already prepared
    if ((_dbPath != nil) &&
        ([[NSFileManager defaultManager] fileExistsAtPath:_dbPath]))
    {
        return YES;
    }

    // db in main bundle - cant edit.  copy to library if !exist
    NSString *dbTemplatePath = [[NSBundle mainBundle] pathForResource:@"contacts" ofType:@"db"];
    NSLog(@"%@", dbTemplatePath);

    NSString *libraryPath = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) lastObject];
    _dbPath = [libraryPath stringByAppendingPathComponent:@"contacts.db"];

    NSLog(@"dbPath: %@", _dbPath);

    // copy db from template to library
    if (![[NSFileManager defaultManager] fileExistsAtPath:_dbPath])
    {
        NSLog(@"db not exists");
        NSError *error = nil;
        if (![[NSFileManager defaultManager] copyItemAtPath:dbTemplatePath toPath:_dbPath error:&error])
        {
            return NO;
        }

        NSLog(@"copied");
    }    

    return YES;    
}
于 2012-08-28T19:07:27.617 に答える