将多个sqlite3 db保存到documentsdirectory

时间:2011-12-01 01:31:20

标签: iphone sqlite

我有一个应用程序,我试图将几个不同的表保存到文档目录。第一个保存很好,但其他人没有。我一直在研究,并没有找到任何说我不能这样做的东西。希望有人能说清楚。我正在使用的代码是典型的“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]);
   }

1 个答案:

答案 0 :(得分:0)

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

是的错误就在这一行:

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

只需交换你很好的参数。

相关问题