数据库未创建

时间:2012-08-28 16:03:51

标签: ios sqlite nsfilemanager

我有一段代码,如果尚未创建数据库,则添加数据库。 这是代码

-(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声明。我需要它来检查所以我可以添加这个数据库

如何让它正常工作?

1 个答案:

答案 0 :(得分:0)

注销路径,这样如果您在模拟器中运行,您可以通过转到终端中的该路径来查看文件是否确实存在。您还可以使用sqlite cmdline检查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;    
}