如何在iOS中覆盖SQLite数据库 [英] How to overwrite a SQLite DB in iOS

查看:91
本文介绍了如何在iOS中覆盖SQLite数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在其中使用SQLlite数据库的地方创建了一个应用程序...如果需要,我可以使用以下方法在应用程序首次启动时将其复制到NSCaches目录中:

i have created an App in where i am using a SQLlite Database...I copy that Database, if needed, in the NSCaches Directory at the first start of the app with the following method:

- (void) copyDatabaseIfNeeded {

//Using NSFileManager we can perform many file system operations.
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
NSString *dbPath = [self getDBPath];
BOOL success = [fileManager fileExistsAtPath:dbPath];

if(!success) {


    NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"datenbankSpeed"];
    success = [fileManager copyItemAtPath:defaultDBPath toPath:dbPath error:&error];

    if (!success)
        NSAssert1(0, @"Failed to create writable database file with message '%@'.", [error localizedDescription]);
}
}

- (NSString *) getDBPath {

//Search for standard documents using NSSearchPathForDirectoriesInDomains
//First Param = Searching the documents directory
//Second Param = Searching the Users directory and not the System
//Expand any tildes and identify home directories.


NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory , NSUserDomainMask, YES);
NSString *documentsDir = [paths lastObject];
return [documentsDir stringByAppendingPathComponent:@"datenbankSpeed"];
}

我的问题是,如果我在数据库文件中更改了某项内容并为我的客户创建了新的应用程序文件,他们会在旧的应用程序上安装新的应用程序,但旧的数据库仍在使用中,这将导致崩溃!

My problem is, if i change sth in the Database- file and create a new App File for my Customers, they install the new App over the old App but the old Database is still in use, which will result in a crash!

推荐答案

我假设问题仅在您更改数据库架构时出现.在sqlite数据库中创建一个版本表,并添加一列schema_version,并使用您代码中的值填充它.现在,每当您更改sql模式时,请在代码中更新schema_version号.在copyDatabaseIfNeeded中,检查是否有现有的db文件,将其打开并读取schema_version.如果此版本与您当前的版本相同,那么就可以了.否则,您需要迁移架构.您可能还希望将数据迁移到新的架构中.

I'm assuming the problem is only when you change your db schema. Create a version table in the sqlite database and add a column schema_version and populate it with a value from your code. Now, whenever you change your sql schema, update schema_version number in your code. In copyDatabaseIfNeeded, check if you have existing db file, open it and read schema_version. If this version is the same as your current version, then you're fine. Else, you need to migrate the schema. You'll probably also want to migrate the data into the new schema as well.

要弄清-在copyDatabaseIfNeeded中,请执行以下操作:

To clarify - in copyDatabaseIfNeeded, do the following:

int version = ... // read schema_version from db
if (version != kCurrentSchemaVersion)
{
    // convert the schema into new version preserving data if required. 
    // this can be a multi-step process. Eg. if user directly upgrades to schema_version 3
    // after schema_version 1, first you'll convert from 1->2, then 2->3. 
}

您可能还想看看@Martin在评论中提到的PRAGMA user_version.

You might also want to take a look at PRAGMA user_version as mentioned by @Martin in the comments.

这篇关于如何在iOS中覆盖SQLite数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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