【问题标题】:iOS download sqlite database and replace existing oneiOS 下载 sqlite 数据库并替换现有的
【发布时间】: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);

【问题讨论】:

    标签: ios sqlite nsurl


    【解决方案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 数据库”。
    猜你喜欢
    • 2017-08-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多