如何将 SQLite 文件中的更改迁移到现有应用程序中? [英] How to migrate changes in SQLite file into an existing app?

查看:49
本文介绍了如何将 SQLite 文件中的更改迁移到现有应用程序中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我提交了一个带有 SQLite 数据库文件的 iOS 应用程序,用于在应用程序内存储数据.

I've submitted an iOS app with SQLite database file to store data inside the app.

现在在应用的下一个版本中 - 我对数据库文件进行了一些修改.

Now in next version of the app – I have some modifications in a database file.

喜欢

  1. 添加了新表(1 个空表,2 个固定数据)
  2. 在表中添加了新列
  3. 从表中删除现有列

我在想什么?

我将在我的应用程序中添加一个新的数据库文件(包括所有更改),在应用程序的第一次运行(更新后)我将检查旧数据库文件是否存在,如果可用,将附加所有内容从旧文件到新文件,最后删除旧文件.

I'll add a new database file (which included all changes) in my app, at first run of the app (after update) I'll check for the existence of an old database file, if available, will append everything from old to new file and lastly will delete the old file.

这是正确的方式还是可以有更重要的方式,如核心数据迁移或其他更合适的方式?

Is this the right way or there can be a more significant way like Core Data migration Or other more appropriate way?

推荐答案

我不知道是否存在正确的方法,但我在这种情况下的经验是创建一个 sql 迁移脚本并使用 pragma user_version 控制数据库版本sqlite.

I dont know if exists a right way, but my experience in this case, was to create a sql migration script and control the database version with pragma user_version of the sqlite.

我的应用程序有三个数据库更改,然后我有三个 sql 迁移脚本.当应用程序启动时,存在一个 if 来检查数据库 user_version,然后我应用数据库更新所需的脚本.

My app has three database changes, then I have three sql migration scripts. When the app is started exists a if to check the database user_version, then I apply the needed scripts for the database update.

在此脚本中,我创建新表,获取旧表的信息,将数据插入最近创建的表中,并在脚本末尾设置 user_version:

In this scripts, I create the new tables, I get information of the old table, I insert the data in the recently created tables and in the end of script I set the user_version:

//这个数字是数据库的版本:1,2,3...PRAGMA user_version = 2;.

要知道用户拥有哪个版本,我执行查询PRAGMA user_version;.

To know which version the user has, I do the query PRAGMA user_version;.

希望能帮到你.

在 USER 表中添加字段登录":

Field "login" added in USER table:

//Create the new table
CREATE TABLE user_new (
    id TEXT PRIMARY KEY,
    login TEXT,
    name TEXT
);

//Get data of older table
INSERT INTO user_new
    SELECT
        id,
        null AS login,
        name
    FROM
        user;

//Delete te older table
DROP TABLE user;

//Rename the new table
ALTER TABLE user_new RENAME TO user;

//Set database version
PRAGMA user_version = 2;

这篇关于如何将 SQLite 文件中的更改迁移到现有应用程序中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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