简体   繁体   English

Android:onUpgrade不调用数据库升级

[英]Android: onUpgrade not calling at database upgrade

I am developing the second version of my application. 我正在开发应用程序的第二个版本。 In which I am facing a problem. 我正面临一个问题。

In my application the database is in the assets folder. 在我的应用程序中,数据库位于资产文件夹中。 Now I want to update my database from the assets folder in the second version. 现在,我想从第二个版本中的assets文件夹更新数据库。

I tried the code to upgrade as : 我尝试将代码升级为:

// Data Base Version.
private static final int DATABASE_VERSION = 2;

I changed the version from 1 to 2. 我将版本从1更改为2。

//Constructor //构造函数

public DataBaseHelperClass(Context myContext) {     
    super(myContext, DATABASE_NAME, null ,DATABASE_VERSION);
    this.context = myContext;
    //The Android's default system path of your application database.
    DB_PATH = "/data/data/com.example.myapp/databases/";
}

// Create data base //创建数据库

public void createDataBase() throws IOException{
    //check if the database exists
    boolean databaseExist = checkDataBase();

    if(databaseExist){
        this.getWritableDatabase();
    }else{
        this.getReadableDatabase();
        try{
            copyDataBase();
        } catch (IOException e){
            throw new Error("Error copying database");
        }
    }// end if else dbExist
} // end createDataBase().

// Check database //检查数据库

public boolean checkDataBase(){
    File databaseFile = new File(DB_PATH + DATABASE_NAME);
    return databaseFile.exists();        
}

// Copy database from assets //从资产复制数据库

private void copyDataBase() throws IOException{ 
    //Open your local db as the input stream
    InputStream myInput = context.getAssets().open(DATABASE_NAME); 
    // Path to the just created empty db
    String outFileName = DB_PATH + DATABASE_NAME; 
    //Open the empty db as the output stream
    OutputStream myOutput = new FileOutputStream(outFileName); 
    //transfer bytes from the input file to the output file
    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(); 
}

// Open database //打开数据库

public void openDataBase() throws SQLException{      
    //Open the database
    String myPath = DB_PATH + DATABASE_NAME;
    sqliteDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);  
}

// Close database //关闭数据库

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

@Override
public void onCreate(SQLiteDatabase db) {
    // No need to write the create table query.
    // As we are using Pre built data base.
    // Which is ReadOnly.
}

// On Update database. //在更新数据库上。

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    if(newVersion>oldVersion){
        context.deleteDatabase(DATABASE_NAME);
        try {
            copyDataBase();
        } catch (IOException e) {
            e.printStackTrace();
        }
    } else {

    }
}

But the issue is that, the database is not updated after executing the apk in the device. 但是问题是,在设备中执行apk后数据库没有更新。 Now what should I do. 现在我该怎么办。

My applications first version is on the market. 我的应用程序第一个版本在市场上。 Don't know how to update database in the second version. 不知道如何在第二版本中更新数据库。

Please guide me with your valuable suggestions, I am in middle of my application and I am not able to move. 请为我提供您的宝贵建议,我处于申请的中间位置,无法搬迁。

EDIT : onUpgrade not calling. 编辑: onUpgrade不调用。 As i tried to print log in 1st line in onUpgrade but it is not printed. 当我尝试在onUpgrade中打印日志的第一行时,却没有打印出来。 So the issue is that, onUpgrade is not calling. 因此,问题在于onUpgrade没有调用。

In order to upgrade your database you need to do properly by manipulating the schema I found Both the answers for my questions were useful... 为了升级您的数据库,您需要通过操纵我发现的模式来正确地做这两个问题的答案都很有用...

Upgrading database version automatically 自动升级数据库版本

This is mostly answered in this link: Why is onUpgrade() not being invoked on Android sqlite database? 多数在此链接中得到解答: 为什么未在Android sqlite数据库上调用onUpgrade()?

onUpgrade() will get called in getWriteableDatabase() or getReadableDatabase() , so if you never open the DB for writing or reading specifically, you'd have to manually call through to onUpgrade() or open your database. onUpgrade()将在getWriteableDatabase()getReadableDatabase()被调用,因此,如果您从不打开专门用于读写的数据库,则必须手动调用onUpgrade()或打开数据库。

To test your onUpgdate you should: 要测试onUpgdate,您应该:

  1. install old version of your app. 安装旧版本的应用程序。 launch it and let it create a db. 启动它,并让它创建一个数据库。
  2. install new version of your app. 安装新版本的应用程序。 This should be done with a special adb command: adb install -r . 这应该使用特殊的adb命令完成: adb install -r -r key simulates app update process. -r键模拟应用程序更新过程。 Without -r key adb install just deletes old version and intalls new one, so onUpgrade doesn't called. 没有-r键, adb install只会删除旧版本并安装新版本,因此不会调用onUpgrade。

But you better write a test for this purpose. 但是您最好为此目的编写一个测试。 Google it. 去谷歌上查询。

I got my answer. 我得到了答案。 The changes I have done are in constructor only as : 我所做的更改仅在构造函数中为:

public DataBaseHelperClass(Context context) {     
super(context, DATABASE_NAME, null ,DATABASE_VERSION);
this.myContext = context;
//The Android's default system path of your application database.
DB_PATH = "/data/data/com.example.myapp/databases/";
}

and it works for me. 它对我有用。

If we use exist database by copy method, we have to set the current DB version to SQLiteDatabase instance. 如果我们通过复制方法使用现有数据库,则必须将当前数据库版本设置为SQLiteDatabase实例。 And each time you want upgrade app call getWritableDatabase(), the onUpgrade() function will be called. 每次您要升级应用程序调用getWritableDatabase()时,都会调用onUpgrade()函数。

public void createDataBase() throws IOException {
    boolean dbExist = checkDataBase();
    Logger.debug(TAG, "checkDataBase dbExist: " + dbExist); 
    if (dbExist) {
        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.getWritableDatabase();

        try {

            copyDataBase();
            Logger.debug(TAG, "copyDataBase SUCCESS"); 
        } catch (IOException e) {

            throw new Error("Error copying database");

        }
    }


public SQLiteDatabase openDataBase() throws SQLException {
     String path = mContext.getDatabasePath(DB_NAME).getPath();
    mDataBase = SQLiteDatabase.openDatabase(path, null,
            SQLiteDatabase.OPEN_READWRITE);
    mDataBase.setVersion(DB_VERSION);
    return mDataBase;

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM