【问题标题】:how to download update from iphone app to replace old sqlite database如何从 iphone 应用程序下载更新以替换旧的 sqlite 数据库
【发布时间】:2010-04-17 12:23:43
【问题描述】:

我在我的 iphone 应用上使用 sqlite 数据库,并且

我需要通过我的服务器从 Internet 更新此数据库

如何下​​载新数据库并删除旧数据库

并将新数据库重新复制到文档目录

【问题讨论】:

    标签: iphone database sqlite database-connection


    【解决方案1】:
    1. 使用 +[NSData dataWithContentsOfURL:] 下载您的文件。
    2. 关闭 sqlite DB(如果已打开)。
    3. 使用 -[NSFileManeger removeItemAtPath:error:] 删除旧的数据库文件(可能只是重命名 - 如果下载的数据出现问题,您可以恢复到以前版本的数据库故障)
    4. 使用 -[NSData writeToFile:atomically:] 将下载的数据写入 DB 文件。

    【讨论】:

      【解决方案2】:

      这是完整的代码

      //替换我从应用程序通过电子邮件发送的数据库。

      -(void)handleOpenURL:(NSURL *)url {

      NSFileManager *fileManager = [NSFileManager defaultManager];
      NSError *error;
      NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
      NSString *documentsDirectory = [paths objectAtIndex:0];
      
      NSString *txtPath = [documentsDirectory stringByAppendingPathComponent:@"/DemoApp.sqlite"];
      
      
      NSURL *newUrl = [[NSURL alloc] initWithString:
                    [txtPath stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding]];
      
      
      if ([fileManager fileExistsAtPath:txtPath] == NO) {
          [fileManager copyItemAtURL:url toURL:newUrl error:&error];
      }
      else if ([fileManager fileExistsAtPath:txtPath] == YES) {
      
          [fileManager removeItemAtPath:txtPath error:&error];
           [fileManager copyItemAtURL:url toURL:newUrl error:&error];
      }
      

      }

      希望对你有帮助

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-07-06
        • 2011-10-05
        • 2011-08-02
        • 2011-03-07
        • 2014-04-28
        • 2011-11-06
        • 2013-08-17
        • 1970-01-01
        相关资源
        最近更新 更多