刷新设备和应用商店上的SQLITE3(Core Data) [英] refresh SQLITE3 (Core Data) on device and app store

查看:124
本文介绍了刷新设备和应用商店上的SQLITE3(Core Data)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序利用核心数据SQLITE3在模拟器中完美的工作。然而我不明白如何更新设备上的数据库,我想是相同的在应用商店。



我从应用程序中的.txt文件更新数据库并创建数据库,此功能仅用于创建数据库,并将在最终版本中删除。我的想法是在模拟器中创建数据库,锁定更新部分的代码,然后分发包更新的数据库。



但是,当我重建我的应用程序在设备上,它仍然有数据库中的旧数据。



我一直在四处看,但我恐怕我不完全明白如何解决这个问题。我找到了此主题:无法刷新i​​phone sqlite3数据库



我非常感谢,如果一个好的人可以分享一些光,并帮助我解决这个问题。



p>

解决方案

是否将db文件从bundle目录(只读)复制到可写的文件? (像每个应用程序的文档目录?)。



尝试保存在设备中时,是否收到类似这样的sqlite错误?

  SQLITE_READONLY 8 / *尝试写一个只读数据库* / 



编辑: p>

主包中的所有文件都是只读的,因此如果需要修改其中的一个/某些,则需要在可写的位置复制这些文件。假设你已经调用db mydb.sqlite这里是一些代码复制db(只有当它不存在)到文档目录。

  NSArray * paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES); 
NSString * docDirectory =([paths count]> 0)? [paths objectAtIndex:0]:nil;
NSFileManager * fm = [NSFileManager defaultManager];
NSString * docPath = [docDirectory stringByAppendingPathComponent:@mydb.sqlite];
if(![fm fileExistsAtPath:docPath]){//文件不存在,复制它
NSString * bundlePath = [[NSBundle mainBundle] pathForResource:@mydbofType:@sqlite] ;
NSError * error = nil;
BOOL res = [fm copyItemAtPath:bundlePath toPath:docPath error:& error];
if(!res){
//做错误的事情
}
}


I have an app leveraging Core Data SQLITE3 that works perfectly in the simulator. However i do not understand how to update the DB on the device, which i guess is the same as in app-store.

I update the DB from .txt files in the app and create the DB, this function is there only for creating the DB and will be removed in the final version. My idea is to create the DB in the simulator, lock the update part of the code and then distribute the package with an updated database.

However, when i rebuild my app on the device it still have the old data in the DB.

I have been looking around but i am afraid i do not fully understand how to solve this. I did find this thread: Can't refresh iphone sqlite3 database

I would very much appreciate if some nice person could share some light on this and help me to solve this.

Cheers

解决方案

Have you copied the db file from the bundle directory (which is read only) to a writable one? (like the documents directory of each application?).

When trying to save in the device did you get a sqlite error like this?

SQLITE_READONLY     8   /* Attempt to write a readonly database */

EDIT:

All the files in the main bundle are read only, so if you need to modify one/some of them, you need to copy the files in a location that is writable. Assuming you have called the db mydb.sqlite here is some code that copies the db (only if it does not exists) to the documents directory.

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *docDirectory = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
    NSFileManager *fm = [NSFileManager defaultManager];
    NSString *docPath = [docDirectory stringByAppendingPathComponent:@"mydb.sqlite"];
    if (![fm fileExistsAtPath:docPath]) { // file does not exists, copy it
        NSString *bundlePath = [[NSBundle mainBundle] pathForResource:@"mydb" ofType:@"sqlite"];
        NSError *error = nil;
        BOOL res = [fm copyItemAtPath:bundlePath toPath:docPath error:&error];
        if (!res) {
            // do something with error
        }
    }

这篇关于刷新设备和应用商店上的SQLITE3(Core Data)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆