困惑:如何SQLiteOpenHelper onUpgrade()的行为?而与旧的数据库备份导入在一起? [英] Confusion: How does SQLiteOpenHelper onUpgrade() behave? And together with import of an old database backup?

查看:192
本文介绍了困惑:如何SQLiteOpenHelper onUpgrade()的行为?而与旧的数据库备份导入在一起?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们假设我有一个数据库表TEST_TABLE 2列和相应的SQLiteOpenHelper创建脚本:

  DB_VERSION = 1:
公共无效的onCreate(SQLiteDatabase DB)
{
db.execSql(CREATE TABLE TEST_TABLE(COL_A,COL_B);
}
 

这是最初的应用程序版本1,这是发表在Play商店。

过了一会儿,有一个更新的应用程序和使用数据库。 我猜SQLiteOpenHelper类必须适应这样的:

  DB_VERSION = 2:
公共无效的onCreate(SQLiteDatabase DB)
{
db.execSql(CREATE TABLE TEST_TABLE(COL_A,COL_B,COL_C));
}

公共无效onUpgrade(SQLiteDatabase分贝,INT oldVersion,诠释静态网页)
{
db.execSql(ALTER TABLE TEST_TABLE添加列COL_C);
}
 

经过一段时间后,其他应用程序的更新:

  DB_VERSION = 3:
公共无效的onCreate(SQLiteDatabase DB)
{
db.execSql(CREATE TABLE TEST_TABLE(COL_A,COL_B,COL_C,COL_D));
}

公共无效onUpgrade(SQLiteDatabase分贝,INT oldVersion,诠释静态网页)
{
db.execSql(ALTER TABLE TEST_TABLE添加列COL_D);
}
 

- >这是我需要的意见。 如果用户安装的应用程序版本1,他有列A和B. 如果他再更新到第2版,onUpgrade火灾和添加一列C. 谁从头开始安装新的用户就可以通过创建语句的3列。 如果用户然后更新版本3,onUpgrade再次闪光和列D被加入。 但是,如果用户安装的应用程序版本1,然后跳过第2版和更新版本3的更新?然后,他已经错过了

 公共无效onUpgrade(SQLiteDatabase分贝,INT oldVersion,诠释静态网页)

    {
    db.execSql(ALTER TABLE TEST_TABLE添加列COL_C);
    }
 

部分只有

 公共无效onUpgrade(SQLiteDatabase分贝,INT oldVersion,诠释静态网页)

    {
    db.execSql(ALTER TABLE TEST_TABLE添加列COL_D);
    }
 

将被称为,这导致了表TEST_TABLE(COL_A,COL_B,COL_D)??

什么是处理的实时应用程序,数据库升级的正确方式,这样用户就不会失去自己的数据? 你必须检查在onUpgrade()方法的所有可能的(旧)版本,并执行基于该版本不同的ALTER TABLE语句?

我问,因为在我的应用程序,用户导出和导入数据的可能性,这比出口罢了:复制整个数据库的路程,导入:更换备份副本数据库应用程序的数据库

如果用户有应用程序版本1,出口的数据库,升级应用程序(新的数据库结构)和进口的旧版本1的备份会发生什么情况? - >如何将SQLiteOpenHelper的行为? - >什么用导入/导出功能是正确的方法来处理数据库升级已在一起

解决方案
  

什么是处理的实时应用程序,数据库升级的正确方式,这样用户就不会失去自己的数据?你必须检查在onUpgrade()方法的所有可能的(旧)版本,并执行基于该版本不同的ALTER TABLE语句?

总的来说,是的。

一个常用方法,这是做成对升级:

 公共无效onUpgrade(SQLiteDatabase分贝,INT oldVersion,诠释静态网页){
  如果(oldVersion 2){
    //做升级从1至2
  }

  如果(oldVersion 3;){
    //不从2升级到3,这​​也将包括1→3,
    //因为你刚升级1→2
  }

  // 等等
}
 

这大致相当于Rails迁移,例如。

  

如果用户有应用程序版本1,出口的数据库,升级应用程序(新的数据库结构)和进口的旧版本1的备份会发生什么情况? - > SQLiteOpenHelper将如何表现

如果通过复制整个数据库走,你从字面上的意思是SQLite数据库文件的完整文件副本,然后当 SQLiteOpenHelper 去打开还原的备份,它将数据库具有旧的架构版本,将通过 onUpgrade()正常。

  

什么是正确的方式来处理数据库的升级与导入/导出功能组合在一起?

我怀疑,答案是:要么让你的备份复制整个文件,或者还安排备份与恢复架构的版本,您可以通过调用的getVersion() A SQLiteDatabase 对象。话虽这么说,我还没有处理过这种情况不多,可能会有更多的问题,我没有想到的。

let's assume I have a database table test_table with 2 columns and a corresponding create script in the SQLiteOpenHelper:

DB_VERSION = 1:
public void onCreate(SQLiteDatabase db)
{
db.execSql("CREATE table test_table (COL_A, COL_B);
}

This is the initial app version 1, which is published in the Play Store.

After a while there's an update to the app and the utilized database. I guess the SQLiteOpenHelper class has to be adapted like this:

DB_VERSION = 2:
public void onCreate(SQLiteDatabase db)
{
db.execSql("CREATE table test_table (COL_A, COL_B, COL_C)");
}

public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
db.execSql("ALTER TABLE test_table ADD Column COL_C");
}

After some time, another app update:

DB_VERSION = 3:
public void onCreate(SQLiteDatabase db)
{
db.execSql("CREATE table test_table (COL_A, COL_B, COL_C, COL_D)");
}

public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
db.execSql("ALTER TABLE test_table ADD Column COL_D");
}

--> This is where I need advice. If the user installs app version 1, he has Columns A and B. If he then updates to version 2, onUpgrade fires and adds a column C. New users who install from scratch get the 3 columns via the create statement. If the user then updates to version 3, onUpgrade fires again and a column D is added. But WHAT IF the user installs app version 1, then skips the update of version 2 and updates version 3? Then he would have missed the

public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)

    {
    db.execSql("ALTER TABLE test_table ADD Column COL_C");
    }

part and only

public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)

    {
    db.execSql("ALTER TABLE test_table ADD Column COL_D");
    }

would be called, which leads to a table test_table(COL_A, COL_B, COL_D)??

What's the correct way of handling database upgrades of a live app, so the user doesn't lose his data? Do you have to check all possible (old) versions in the onUpgrade() method and execute different alter table statements based on that version?

I am asking because in my app, the user has the possibility to export and import the data, which is nothing more than export: copy the whole database away and import: replace the app database with the backup copy database.

What happens if the user has app version 1, exports the database, upgrades the app (new database structure) and imports the old version 1 backup? --> How will SQLiteOpenHelper behave? --> What is the correct way to handle db upgrades together with import/export functionality?

解决方案

What's the correct way of handling database upgrades of a live app, so the user doesn't lose his data? Do you have to check all possible (old) versions in the onUpgrade() method and execute different alter table statements based on that version?

By and large, yes.

A common approach to this is to do pair-wise upgrades:

public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  if (oldVersion<2) {
    // do upgrade from 1 to 2
  }

  if (oldVersion<3) {
    // do upgrade from 2 to 3, which will also cover 1->3,
    // since you just upgraded 1->2
  }

  // and so on
}

This roughly equates to Rails migrations, for example.

What happens if the user has app version 1, exports the database, upgrades the app (new database structure) and imports the old version 1 backup? --> How will SQLiteOpenHelper behave?

If by "copy the whole database away", you literally mean a full file copy of the SQLite database file, then when SQLiteOpenHelper goes to open the restored backup, it will that the database has the old schema version and will go through onUpgrade() as normal.

What is the correct way to handle db upgrades together with import/export functionality?

I suspect the answer is: either make your backup by copying the entire file, or also arrange to backup and restore the schema version, which you can get by calling getVersion() on a SQLiteDatabase object. That being said, I haven't dealt with this scenario much, and there may be more issues that I am not thinking of.

这篇关于困惑:如何SQLiteOpenHelper onUpgrade()的行为?而与旧的数据库备份导入在一起?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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