使用不同的sqlite db结构将新版本更新到应用商店 [英] Updating new version to app store with different sqlite db structure

查看:21
本文介绍了使用不同的sqlite db结构将新版本更新到应用商店的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在应用商店(1.0 版)上传了一个应用.我的应用程序使用 sqlite 数据库来存储一些数据.现在,我对数据库进行了一些更改(我在数据库中的一个表中添加了 2 或 3 个新列).我想用新版本 1.1(具有不同的数据库结构)更新我的应用程序的先前版本.现在,当已经使用 1.0 版的用户将应用程序升级到 1.1 版时,该数据库已经存在于应用程序沙箱中,因此该应用程序指向 1.1 版,但我的数据库仍然是旧的.我想拥有带有旧数据的新数据库(如果有).请帮帮我.谢谢.

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.

推荐答案

sqlite 支持称为 user_version 属性的东西,您可以执行 PRAGMA user_version 来查询应用程序数据库的当前架构版本.此查询可能会在您的应用启动时写入.

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.

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

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

无论何时创建sqlite db,最好都放置这个属性user_version,以便以后升级时可以查询当前值.检查它需要什么并执行剩余的更改或创建表以升级您的架构版本.

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.

例如:

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

In first release I create table1 with col1, col2

我执行 sql 来创建 table1,一旦它成功完成,我执行 pragma user_version = 1.所以这将表明我当前的架构版本是 1

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

在以后的版本中,我添加了新的 col3,我需要将架构版本更改为 2

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

我首先查询user_version,检查它的值,如果它是1,那么你需要运行alter脚本来添加新列并将用户版本设置为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,因此很难区分新安装和升级方案.所以现在你可以假设如果 db 存在它是升级场景并执行更改脚本,如果不存在则假设它是一个新的安装场景并运行创建脚本.但是看看你是否可以使用上面的 pragma 至少在将来解决你的问题.

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.

这篇关于使用不同的sqlite db结构将新版本更新到应用商店的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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