更新新版本到app store,使用不同的sqlite db结构

时间:2021-12-07 05:34:53

I have uploaded an app on app store (version 1.0). My app is using a sqlite database for storing some data. Now, I have made some changes in the database (I have added 2 or 3 new columns in one of the tables in my db). I want to update the previous version of my app with the new version 1.1 (which is having different db structure). Now, when the users which are already using the version 1.0 upgrade the app to version 1.1, the db is already present in app sandbox and hence, the app is pointing to version 1.1, but my db is still the old one. I want to have the new db with the old data, if any. Please help me out. Thanks.

我在app store上上传了一个app(1.0版)。我的app使用sqlite数据库存储一些数据。现在,我对数据库做了一些更改(我在db中的一个表中添加了2或3个新列)。我想用新版本1.1(具有不同的db结构)来更新我的应用程序的上一个版本。现在,当使用1.0版本的用户将app升级到1.1版本时,db已经出现在app sandbox中,因此,app指向1.1版本,但我的db仍然是旧版本。我想要新的db和旧的数据,如果有的话。请帮帮我。谢谢。

2 个解决方案

#1


12  

sqlite supports something called as user_version property, you can execute PRAGMA user_version to query for current schema version of your app db. This query can happen write at the beginning when your app starts.

sqlite支持user_version属性,可以执行PRAGMA user_version来查询app db的当前模式版本。这个查询可以在应用程序启动时开始编写。

to update this user_version execute following update query PRAGMA user_version = version_num;

要更新这个user_version,请执行以下更新查询PRAGMA user_version = version_num;

Whenever you create sqlite db, it is best practice to put this property user_version, so that when you are upgrading in future you can query current value. Check what it's needs to be and execute remaining alter or create tables to upgrade your schema version.

每当创建sqlite db时,最好将这个属性user_version放到其中,以便将来升级时可以查询当前值。检查需要做什么,并执行剩下的修改或创建表来升级您的模式版本。

For example:

例如:

In first release I create table1 with col1, col2

在第一个版本中,我使用col1和col2创建了table1

I execute sql to create table1 and once it is successfully done, i execute pragma user_version = 1. so this will indicate my current schema version is 1

我执行sql创建表1,一旦成功完成,我执行pragma user_version = 1。这表明我当前的模式版本是1

In future release i add new col3, i need to change my schema version to 2

在未来的版本中,我添加了新的col3,我需要将模式版本更改为2

I first query user_version, check it's value and if it is 1, then you need to run alter script to add new column and set user version to 2.

我首先查询user_version,检查它的值,如果是1,那么需要运行alter脚本添加新列并将user version设置为2。

In your case, since you haven't set the user_version before, it would be difficult to differentiate new install vs an upgrade scenario. So for now may be you assume if db is present it is upgrade scenario and execute alter scripts and if not present assume it is a new install scenario and run create scripts. But see if you can use above pragma to solve your problem in future atleast.

在您的例子中,由于您以前没有设置user_version,所以很难区分新的安装和升级场景。因此,现在您可能会假定,如果db是当前的升级场景,并执行alter脚本,如果不存在,则假定它是一个新的安装场景,并运行创建脚本。但至少看看你是否可以用上面的实用程序来解决你的问题。

#2


-1  

You could check on launch if it's the old db, and if so have a routine to create a new db with the new structure (with a temporary name), copy all the data from the old to the new, close the old db & delete it, close the new db, rename it, then finally open it again for your updated app to use. Easy, fast, & you don't need an in-depth knowledge of SQLite to do it.

你可以检查发射如果是旧的数据库,如果是有一个程序来创建一个新的数据库与新结构(一个临时名称),复制所有数据从旧到新的,关闭旧db &删除它,关闭新db,重命名它,最后再打开它为你更新的应用程序使用。简单,快速,你不需要对SQLite有深入的了解。

#1


12  

sqlite supports something called as user_version property, you can execute PRAGMA user_version to query for current schema version of your app db. This query can happen write at the beginning when your app starts.

sqlite支持user_version属性,可以执行PRAGMA user_version来查询app db的当前模式版本。这个查询可以在应用程序启动时开始编写。

to update this user_version execute following update query PRAGMA user_version = version_num;

要更新这个user_version,请执行以下更新查询PRAGMA user_version = version_num;

Whenever you create sqlite db, it is best practice to put this property user_version, so that when you are upgrading in future you can query current value. Check what it's needs to be and execute remaining alter or create tables to upgrade your schema version.

每当创建sqlite db时,最好将这个属性user_version放到其中,以便将来升级时可以查询当前值。检查需要做什么,并执行剩下的修改或创建表来升级您的模式版本。

For example:

例如:

In first release I create table1 with col1, col2

在第一个版本中,我使用col1和col2创建了table1

I execute sql to create table1 and once it is successfully done, i execute pragma user_version = 1. so this will indicate my current schema version is 1

我执行sql创建表1,一旦成功完成,我执行pragma user_version = 1。这表明我当前的模式版本是1

In future release i add new col3, i need to change my schema version to 2

在未来的版本中,我添加了新的col3,我需要将模式版本更改为2

I first query user_version, check it's value and if it is 1, then you need to run alter script to add new column and set user version to 2.

我首先查询user_version,检查它的值,如果是1,那么需要运行alter脚本添加新列并将user version设置为2。

In your case, since you haven't set the user_version before, it would be difficult to differentiate new install vs an upgrade scenario. So for now may be you assume if db is present it is upgrade scenario and execute alter scripts and if not present assume it is a new install scenario and run create scripts. But see if you can use above pragma to solve your problem in future atleast.

在您的例子中,由于您以前没有设置user_version,所以很难区分新的安装和升级场景。因此,现在您可能会假定,如果db是当前的升级场景,并执行alter脚本,如果不存在,则假定它是一个新的安装场景,并运行创建脚本。但至少看看你是否可以用上面的实用程序来解决你的问题。

#2


-1  

You could check on launch if it's the old db, and if so have a routine to create a new db with the new structure (with a temporary name), copy all the data from the old to the new, close the old db & delete it, close the new db, rename it, then finally open it again for your updated app to use. Easy, fast, & you don't need an in-depth knowledge of SQLite to do it.

你可以检查发射如果是旧的数据库,如果是有一个程序来创建一个新的数据库与新结构(一个临时名称),复制所有数据从旧到新的,关闭旧db &删除它,关闭新db,重命名它,最后再打开它为你更新的应用程序使用。简单,快速,你不需要对SQLite有深入的了解。