Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/118.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 未创建数据库_Ios_Sqlite_Nsfilemanager - Fatal编程技术网

Ios 未创建数据库

Ios 未创建数据库,ios,sqlite,nsfilemanager,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, NSUserDom

我有一段代码,如果还没有创建数据库,它会添加一个数据库。 这是密码

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


如何使其正常工作?

注销该路径,这样,如果您正在emulator中运行,您可以通过在终端中转到该路径来查看文件是否确实存在。还可以使用sqlite cmdline检查数据库文件

下面是我的一个示例中执行类似任务的代码

它(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)
处停止,然后跳过其余部分,而不会出现任何错误hmm-你能尝试添加一组日志并在模拟器和设备中运行吗?(试图排除eumlator中的文件系统/预授权等)另外-当您在emulator中运行时,请确保您可以注销并访问终端中的该路径。dbPath的日志似乎很好。它仍然停留在该行并跳过该方法中的其余代码。我在我的另一个应用程序中使用了这段代码,我试图通过向这个项目添加一个数据库并复制代码来复制它,但它不起作用。我修复了它。我只需要删除文件,让代码替换它。谢谢你的帮助