java - 如何在不更新 Android 应用程序的情况下升级数据库

标签 java android database sqlite

在我的应用程序中,我有一个 dbhelper 类,其中数据库中的所有内容都会发生,我知道我必须使用 onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) 但我遇到了问题了解如果我不更新完整的应用程序,我该如何调用它,我想要的只是在互联网通话中发现我已经更改了我的网络表中的版本以删除我的应用程序中的表并重建它们,以便它可以获得新的信息。 这是我的代码:

在我的 bd 助手中:

public class CuestionarioHelper extends SQLiteOpenHelper {

private static String DB_PATH = "/data/data/myapp.VerCuestionarioEspanol_copy/databases/";
private static final String DB_NAME="cuestionario.db";
private static final int SCHEMA_VERSION=1;
private SQLiteDatabase myData; 
public CuestionarioHelper(Context context) {
    super(context, DB_NAME, null, SCHEMA_VERSION);
}   
@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL("CREATE TABLE Version (_id INTEGER PRIMARY KEY AUTOINCREMENT, version TEXT);");
    db.execSQL("CREATE TABLE Cuestionario (_id INTEGER PRIMARY KEY AUTOINCREMENT, pregunta TEXT, respuesta TEXT);");
    }
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE IF EXISTS " + "Version");
    db.execSQL("DROP TABLE IF EXISTS " + "Cuestionario");
    onCreate(db);
}

在我的 mainActivity 中:

private CuestionarioHelper db = null;

 @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

   db = new CuestionarioHelper(this);

// here in a  class DownloadFileAsync extends AsyncTask<String, String, String> 
  I download the version of the table in the web and if is different than the one 
  already in the db then it calls the table complete information to replace the one 
  in the apps db.

//Here I verify what table version I have so I can add to a non existing table, 
  do nothing if both versions are the same or replace the bd table if they are different.

public void verficar(){

verify = db.getTableCount();
if(verify == 0){
    tvVersion.setText("Version nueva "+Version.get(0).version);
    // download the table information and added to the db
}else{
    if(versionEntrada.equals(db.getVersion())){
        tvVersion.setText("Version ya existe "+db.getVersion());
           //version is the same do nothing
    }else{
        int oldVersion = Integer.parseInt(db.getVersion());
        int newVersion = Integer.parseInt(versionEntrada);
        db.onUpgrade(db, oldVersion, newVersion);//here is where I don't know 
                                                    //how to call it, this is not working!
        // download the table information and added to the db
        tvVersion.setText("Version actualizada "+Version.get(0).version);
    }
}
}

在数据库中,版本表是我存储表版本的地方,或者它可能是数据库版本,它只有一列和一行,因为它只是一个数字来与我的网络表版本进行比较。

最佳答案

您应该将 SCHEMA_VERSION 设置为非最终版本,并将此版本作为参数传递:

更改这些行

private static final int SCHEMA_VERSION=1;
public CuestionarioHelper(Context context) {
    super(context, DB_NAME, null, SCHEMA_VERSION);
}   

至(请注意 SCHEMA_VERSION 已删除)

public CuestionarioHelper(Context context, int version) {
    super(context, DB_NAME, null, version);
}   

关于java - 如何在不更新 Android 应用程序的情况下升级数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12626431/

相关文章:

android - 如何检测android应用程序何时升级?

android - Realm 打开/关闭实践

java - JDBC获取外键引用的关系+属性

java - 数据库 - 有 2 个表,需要另一个具有 ID 和另一个字段的表

database - 如何在 Spring Data JPA 中保持时间

java - 如何使用 servlet 运行长时间运行的进程

java - 时区不考虑夏令时

java - 垃圾收集时间与对象数量或对象大小有关吗?

java - Java 中重写 equals() 方法 (OCJP)

android ViewPager xml膨胀错误