java - Sqlite 数据库 onUpgrade() 没有被调用

标签 java android sqlite upgrade

我正在开发一个 android 应用程序,我在 Assets 文件夹中使用了现有的 sqlite 数据库。所以我实际上从那个文件夹中复制了数据库。用户还可以保存自己的数据(将任何内容标记为 Collection )。

我在市场上上传了一个版本。现在我想在数据库中添加一些新数据并上传一个新版本。如果用户从市场更新应用程序,我想保留用户数据(来自以前的版本)并添加新数据。我做了一些谷歌,发现升级方法应该可以解决问题。但是我正在从代码中更改 DATABASE_VERSION,但是 on upgrade 方法没有被调用。我想知道我错过了什么。这是我的代码:

public class DataBaseHelper extends SQLiteOpenHelper {

private static String DB_PATH = "/data/data/riskycoder.login/databases/";
public static String DB_NAME = "datenbank_EN.sqlite";
private SQLiteDatabase myDataBase;
private final Context myContext;
private static final int DATABASE_VERSION = 1;





public DataBaseHelper(Context context) {
    super(context, DB_NAME, null, DATABASE_VERSION);
    this.myContext = context;
}

public void createDataBase() throws IOException {
    boolean dbExist = checkDataBase();
    if (dbExist) {
    } else {
        this.getWritableDatabase();
        try {
            this.close();
            copyDataBase();
        } catch (IOException e) {
            throw new Error("Error copying database");
        }
    }

}

private boolean checkDataBase() {
    SQLiteDatabase checkDB = null;
    try {
        String myPath = DB_PATH + DB_NAME;
        checkDB = SQLiteDatabase.openDatabase(myPath, null,SQLiteDatabase.OPEN_READWRITE);
    } catch (SQLiteException e) {
    }
    if (checkDB != null)
        checkDB.close();
    return checkDB != null ? true : false;
}

private void copyDataBase() throws IOException {

    InputStream myInput = myContext.getAssets().open(DB_NAME);
    String outFileName = DB_PATH + DB_NAME;
    OutputStream myOutput = new FileOutputStream(outFileName);
    byte[] buffer = new byte[2048];
    int length;
    while ((length = myInput.read(buffer)) > 0) {
        myOutput.write(buffer, 0, length);
    }
    myOutput.flush();
    myOutput.close();
    myInput.close();

    //myDataBase.setVersion(DATABASE_VERSION);
}

public void openDataBase() throws SQLException {
    String myPath = DB_PATH + DB_NAME;
    myDataBase = SQLiteDatabase.openDatabase(myPath, null,SQLiteDatabase.OPEN_READWRITE);
    Log.d("Test", "Database version: " +myDataBase.getVersion());
}

@Override
public synchronized void close() {
    if (myDataBase != null)
        myDataBase.close();
    super.close();
}



@Override
public void onCreate(SQLiteDatabase db) {
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    Log.d("Test", "in onUpgrade. Old is: " + oldVersion + " New is: " + newVersion);


}

}

最佳答案

onUpgrade() 仅在检测到数据库版本号发生变化时调用。增加数据库版本如下:

private static final int DATABASE_VERSION = 2;

关于java - Sqlite 数据库 onUpgrade() 没有被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10664935/

相关文章:

android - 如何在android中的基本适配器中删除自定义 ListView 中的行

android - android中如何进行同步?

java - 创建 SQLite 数据库时出错 : "android.database.sqlite.SQLiteException: near(9598)"?

android快捷方式,访问启动器数据库

java - SQL复杂选择

java - 使用对象作为数组时出现空指针异常

java - Selenium Webdriver - 从动态表访问数据

android - RecyclerView LinearLayoutManager 设置项目计数

json - python 3 : deserialize nested dictionaries from sqlite

java - LibGDX GUI 纹理打包器无法打开