安卓:onUpgrade没有数据库升级呼叫 [英] Android: onUpgrade not calling at database upgrade

查看:151
本文介绍了安卓:onUpgrade没有数据库升级呼叫的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发我的应用程序的第二个版本。在我面临的一个问题。

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.

我试过code作为升级:

I tried the code to upgrade as :

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

我改变从1版本2。

I changed the version from 1 to 2.

//构造

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 {

    }
}

但问题是,数据库没有在设备执行APK后更新。
现在我该怎么办。

But the issue is that, the database is not updated after executing the apk in the device. 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.

编辑: onUpgrade没有要求。当我试着在onUpgrade第一行打印日志,但它不打印。所以,问题是,onUpgrade并没有叫。

EDIT : onUpgrade not calling. As i tried to print log in 1st line in onUpgrade but it is not printed. So the issue is that, onUpgrade is not calling.

推荐答案

为了升级数据库,你需要通过操纵,我发现无论是对我的问题的答案是非常有用的架构妥善做好...

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...

<一个href=\"http://stackoverflow.com/questions/15862432/upgrading-database-version-automatically\">Upgrading数据库版本自动

这篇关于安卓:onUpgrade没有数据库升级呼叫的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆