android - 即使在增加数据库版本后,SQLite 数据库也不会在从 Play 商店更新应用程序时更新

标签 android sqlite

我陷入了一个令人沮丧的境地。我有一个如下所示的 SQlite 数据库类:

// The Android's default system path of your application database.
private static String DB_PATH = "/data/data/com.example.ABC/databases/";

private static String DB_NAME = "ABCdb.sqlite";

private SQLiteDatabase myDataBase;
private static int DATABASE_VERSION=2;

private final Context myContext;

/**
 * Constructor Takes and keeps a reference of the passed context in order to
 * access to the application assets and resources.
 *
 * @param context
 */
public DBHelper(Context context) {

    super(context, DB_NAME, null, DATABASE_VERSION);
    this.myContext = context;
}

/**
 * Creates a empty database on the system and rewrites it with your own
 * database.
 * */
public void createDataBase() throws IOException {

    boolean dbExist = checkDataBase();

    if (dbExist) {
        // do nothing - database already exist
        // By calling this method here onUpgrade will be called on a
        // writable database, but only if the version number has been increased

        this.getWritableDatabase();
    } else {

        // By calling this method and empty database will be created into
        // the default system path
        // of your application so we are gonna be able to overwrite that
        // database with our database.
        this.getReadableDatabase();

        try {

            copyDataBase();

        } catch (IOException e) {
            System.out.println(e.toString());
            // throw new Error("Error copying database");

        }
    }

}

/**
 * Check if the database already exist to avoid re-copying the file each
 * time you open the application.
 *
 * @return true if it exists, false if it doesn't
 */
private boolean checkDataBase() {

    SQLiteDatabase checkDB = null;

    try {
        String myPath = DB_PATH + DB_NAME;
        checkDB = SQLiteDatabase.openDatabase(myPath, null,
                SQLiteDatabase.OPEN_READWRITE);

    } catch (SQLiteException e) {

        // database does't exist yet.

    }

    if (checkDB != null) {

        checkDB.close();

    }

    return checkDB != null ? true : false;
}

/**
 * Copies your database from your local assets-folder to the just created
 * empty database in the system folder, from where it can be accessed and
 * handled. This is done by transfering bytestream.
 * */
private void copyDataBase() throws IOException {

    // Open your local db as the input stream
    InputStream myInput = myContext.getAssets().open(DB_NAME);

    // Path to the just created empty db
    String outFileName = DB_PATH + DB_NAME;

    // Open the empty db as the output stream
    OutputStream myOutput = new FileOutputStream(outFileName);

    // transfer bytes from the inputfile to the outputfile
    byte[] buffer = new byte[1024];
    int length;
    while ((length = myInput.read(buffer)) > 0) {
        myOutput.write(buffer, 0, length);
    }

    // Close the streams
    myOutput.flush();
    myOutput.close();
    myInput.close();

}

public SQLiteDatabase openDataBase() throws SQLException {

    // Open the database
    String myPath = DB_PATH + DB_NAME;
    myDataBase = SQLiteDatabase.openDatabase(myPath, null,
            SQLiteDatabase.NO_LOCALIZED_COLLATORS);
    return myDataBase;
}

@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) {
    System.out.println("NewVersion : "+newVersion+", OldVersion : "+oldVersion);
    if(newVersion>oldVersion){
        myContext.deleteDatabase(DB_NAME);
        try {
            copyDataBase();
        } catch (IOException e) {
            e.printStackTrace();
        }
    } 
}

// Add your public helper methods to access and get content from the
// database.
// You could return cursors by doing "return myDataBase.query(....)" so it'd
// be easy
// to you to create adapters for your views.

Play 商店上载的应用程序将 DATABASE_VERSION 设置为 1。我在数据库中添加了一些新表。我将 DATABASE_VERSION 增加到 2。但是当我使用调试 keystore 运行应用程序时,会调用 onUpgrade() 方法并调用数据库正在升级,但是当我签署应用程序然后更新已安装的应用程序时,它没有升级数据库。我不知道我是否遗漏了什么。非常感谢任何帮助。

最佳答案

您应该只能使用您应用的另一个调试版本升级您应用的调试版本。发布版本也是如此。发布版本只能用另一个发布版本升级。换句话说 - 只有当您将应用替换为使用相同 key 签名的应用时,这才是升级。

因此,根据您的描述(使用发布版本时无法升级数据库),听起来好像您已将调试版本替换为发布版本。 Android 会将这种情况识别为同一个应用程序,但它会先卸载初始版本(调试),然后将其替换为新版本(发布)。因此,虽然它可能对用户来说是升级,但这实际上是一个卸载/安装场景。

据我所知,这是解释您所见情况的唯一方法。

关于android - 即使在增加数据库版本后,SQLite 数据库也不会在从 Play 商店更新应用程序时更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25945576/

相关文章:

android - Gradle 构建一次发布所有口味

java - 数组中 "string"的索引始终返回 -1,使用 "Arrays.asList"

sql - 如何判断 sqlite 列是否为 AUTOINCREMENT?

sqlite - 在没有任何仪表板的情况下使用 HangFire

python - 插入数百万行的性能

sqlite - sqlite触发器会触发其他触发器吗?

java - onClick 监听器在所有情况下都会导致应用程序崩溃

android - 我的服务没有被破坏 - 如何停止服务

android - 开源安卓启动器列表

android - 在android中使用游标显示来自sqlite数据库的图像