简体   繁体   English

数据库onUpgrade

[英]database onUpgrade

I have a question regarding the upgrade procedure of an App, more specifically, upgrading or changing the database. 我对应用程序的升级过程有疑问,更具体地说,是升级或更改数据库。

Considering I have version 1 of testapp.apk. 考虑到我有testapp.apk的版本1。 This version have DB version 1, the new version of testapp.apk have DB version 2. Now, if I were to upgrade my testapp.apk, will the onUpgrade from the old version get called before the upgrade, or will it simply call the new version´s onUpgrade? 此版本具有数据库版本1,新版本的testapp.apk具有数据库版本2。现在,如果我要升级testapp.apk,将在升级之前调用旧版本的onUpgrade还是直接调用新版本的onUpgrade?

It will run the new version. 它将运行新版本。 While upgrading the app, it doesn't run, it just copies the new app over the old app. 升级应用程序时,它不会运行,只会将新应用程序复制到旧应用程序上。 Then when it runs, it'll only have the new version of the onUpgrade(). 然后,当它运行时,它将仅具有新版本的onUpgrade()。

The onUpgrade code will only be called on the latest installed apk. 仅在最新安装的apk上调用onUpgrade代码。

Your new testapp.apk will be installed first. 您的新testapp.apk将首先安装。 Then, when the database is accessed for the first time, the SQLiteOpenHelper will check the existing version of the database and compare it to the new version you have specified in testapp.apk. 然后,当首次访问数据库时,SQLiteOpenHelper将检查数据库的现有版本,并将其与您在testapp.apk中指定的新版本进行比较。

If the new version is greater than the old version, onUpgrade will be called on the SQLiteOpenHelper in your new testapp.apk. 如果新版本大于旧版本,则将在新testapp.apk中的SQLiteOpenHelper上调用onUpgrade。

Please see my answer to a similar question here as I think it will help explain the mechanics of onCreate/onUpgrade/onDowngrade. 请在此处查看我对类似问题的回答,因为我认为这将有助于解释onCreate / onUpgrade / onDowngrade的机制。

how do i create the database in my android app? 如何在我的Android应用程序中创建数据库?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM