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

时间:2014-09-20 05:30:10

标签: 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商店上传的app已将DATABASE_VERSION设置为1.我在DB中添加了一些新表。我将DATABASE_VERSION增加到2.但是当我使用debug keystore运行应用程序时,调用onUpgrade()方法并调用DB正在升级但是当我签署应用程序然后更新已安装的应用程序时,它不会升级数据库。我不知道我是否遗漏了什么。任何帮助都非常感谢。

1 个答案:

答案 0 :(得分:2)

您应该只能使用应用的其他调试版升级应用的调试版本。发布版本也是如此。发行版只能使用其他发行版升级。换句话说 - 如果您使用已使用相同密钥签名的应用程序替换应用程序,则只是升级。

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

据我所知,这是解释你所见所闻的唯一方法。