加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

iphone – 将更新的数据库复制到ios sdk中的设备中

发布时间:2020-12-14 17:54:25 所属栏目:百科 来源:网络整理
导读:在我的应用程序中,我使用了sqlite DB.我将应用程序的1.0版本提交到了应用程序商店.但之后我对DB进行了一些更改,比如插入一些新行. 然后提交了1.1版本的应用程序.但是我无法获得更新的数据库,因为旧的数据库已经存在于设备中. 我怎么解决这个问题? 如果我删
在我的应用程序中,我使用了sqlite DB.我将应用程序的1.0版本提交到了应用程序商店.但之后我对DB进行了一些更改,比如插入一些新行.

然后提交了1.1版本的应用程序.但是我无法获得更新的数据库,因为旧的数据库已经存在于设备中.

我怎么解决这个问题?

如果我删除了应用程序&然后安装新版本的应用程序,然后我得到了更新的数据库.

//My code for copy of DB is as follow:

- (void)createEditableCopyOfDatabaseIfNeeded
{
    // First,test for existence.
    BOOL success;
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSError *error;
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:@"xxx.sqlite"];
    success = [fileManager fileExistsAtPath:writableDBPath];
    if (success) return;

    // The writable database does not exist,so copy the default to the appropriate location.

    NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"xxx.sqlite"];
    success = [fileManager copyItemAtPath:defaultDBPath toPath:writableDBPath error:&error];
    if (!success) {
        NSAssert1(0,@"Failed to create writable database file with message '%@'.",[error localizedDescription]);
    }
}

我在appDidFinishLaunching方法中调用了这个方法.

当新版本可用时,如何在不删除应用程序的情况下获取更新的数据库

解决方法

您可以使用 NSUserDefaults执行此操作.

在新版本中添加以下代码:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
  if(![[NSUserDefaults standardUserDefaults] boolForKey:@"1.1"]])
  {
     [self removeDatabase];
     [self createEditableCopyOfDatabaseIfNeeded];
     [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"1.1"];
  }
  //Other stuffs
}

删除旧数据库文件的方法:

- (void)removeDatabase
{
    BOOL success;
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSError *error;
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *removeDBPath = [documentsDirectory stringByAppendingPathComponent:@"xxx.sqlite"];
    success = [fileManager fileExistsAtPath:writableDBPath];
    if(success)
    {
       [fileManager removeItemAtPath:writableDBPath error:&error]
    }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读