保存多个sqlite3数据库到documentsdirectory目录



我有一个应用程序,我试图保存几个不同的表文档目录。第一个保存得很好,但其他的保存得不好。我一直在研究,没有发现任何东西说我不能这样做。希望有人能帮上忙。我使用的代码是典型的"CreateEditableCopyOfDatabaseIfNeeded",但就像我说的,我有几个版本的它取决于哪个db我试图保存。下面的代码是第二个不能与"打开DB失败"一起工作的代码提前谢谢。

- (void)createEditableCopyOfScheduleDatabaseIfNeeded
  {
   //test if DB already exist
   BOOL success;
   NSFileManager *fileManager = [NSFileManager defaultManager];
   NSError *error;
   NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,    NSUserDomainMask, YES);
   NSString *documentsDirectory = [paths objectAtIndex:0];
   //save new DB's
   NSString *writableScheduleDBPath = [documentsDirectory stringByAppendingPathComponent:@"schedulelist.sqlite3"];
   NSLog(@"writable Schedule path:%@", writableScheduleDBPath);
   success = [fileManager fileExistsAtPath:writableScheduleDBPath];
   if (!success)
       NSLog(@"Failure to open Schedule DB");
   if (success) return;
   //the writable database does not exist, so copy the default to the appropriate location
   NSString *defaultScheduleDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"schedulelist.sqlite3"];
   NSLog(@"default Schedule path:%@", defaultScheduleDBPath);
   success = [fileManager copyItemAtPath:defaultScheduleDBPath toPath:writableScheduleDBPath error:&error];
   if (!success)
       NSAssert1(0, @"Failed to create Schedule writable database:%@", [error localizedDescription]);
   }

第一次运行时会显示NSLog(@"Failure to open Schedule DB");,因为文档目录中没有数据库。

是的,错误在这一行:

success = [fileManager copyItemAtPath:defaultScheduleDBPath toPath:writableScheduleDBPath error:&error];

只要交换参数就可以了

相关内容

  • 没有找到相关文章

最新更新