iOS 下载 sqlite 数据库并替换现有的

Posted

技术标签:

【中文标题】iOS 下载 sqlite 数据库并替换现有的【英文标题】:iOS download sqlite database and replace existing one 【发布时间】:2012-02-03 17:18:46 【问题描述】:

我必须下载数据库并替换沙箱中的现有数据库: 以下是可能的方法:

DBHelpers *help=[[DBHelpers alloc] init];


    NSString *targetPath=[[help DocumentsDirectory] stringByAppendingPathComponent:DATABASE_NAME];


    NSLog(@"Target path: %@", targetPath);
    NSFileManager *fileManager=[NSFileManager defaultManager];

    //if([fileManager fileExistsAtPath:targetPath])
    //
      //  NSLog(@"Exists");
       // return;
    
    NSString *sourcePath=[help PathForResource:DATABASE_NAME];
    NSLog(@"SourcePath path: %@", sourcePath);
    NSError *error;
  NSData *data=[NSData dataWithContentsOfURL:[NSURL URLWithString:@"www.hello.com/mydb.sqlite"]];
    [data writeToFile:targetPath atomically:NO];

//  [fileManager copyItemAtPath:sourcePath toPath:targetPath error:&error];
    NSLog(@"Error %@", error);

【问题讨论】:

【参考方案1】:

考虑以下步骤:

NSData *fetchedData = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://www.myserver.com/files/DBName.sqlite"]]];
NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDire ctory, NSUserDomainMask, YES) lastObject];
NSString *filePath = [documentsPath stringByAppendingPathComponent:@"DBName.sqlite"];
[fetchedData writeToFile:filePath atomically:YES];

来自iphonedevsdk forum 线程。

【讨论】:

这似乎不再起作用。我在 this question 中做的基本相同,但是当我重新启动我的应用程序时,我收到一条错误消息,提示我正在下载的数据库“似乎不是 SQLite 数据库”。

以上是关于iOS 下载 sqlite 数据库并替换现有的的主要内容,如果未能解决你的问题,请参考以下文章

在 iOS 上使用和访问现有的 SQLite 数据库

如何在 swift 3 中使用现有的 SQLite? (FMDB)

如何引用现有的 SQLite 数据库创建新数据库并插入数据?

在 iPhone 上更新 SQLite 数据库

SpecialFolder.个人现有的sqlite数据库

如何将 SQLite 数据库捆绑并读入 Xcode 11.1?