【问题标题】:Database not being created未创建数据库
【发布时间】:2012-08-23 04:41:21
【问题描述】:

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

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

我怎样才能让它正常工作?

【问题讨论】:

    标签: ios sqlite nsfilemanager


    【解决方案1】:

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

    【讨论】:

    • 在我的代码和您的代码中,它都停在if (fileExistsAtPath),然后跳过其余部分,没有任何错误
    • 嗯——你可以尝试添加一堆日志并在模拟器和设备中运行吗? (试图在 eumlator 中排除文件系统/权限等)
    • 还有——当你在模拟器中运行时,确保你可以注销并在终端中访问该路径。
    • dbPath 的日志看起来不错。它仍然停在该行并跳过该方法中的其余代码。我有这个代码在我的另一个应用程序中工作,我试图通过向这个项目添加一个数据库并复制代码来重现它,但它不工作
    • 我修好了。我只需要删除文件并让代码替换它。感谢您的帮助
    猜你喜欢
    • 2022-10-13
    • 2013-03-07
    • 2014-09-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-02
    相关资源
    最近更新 更多