Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/204.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android:onUpgrade在数据库升级时未调用_Android_Database_Sqlite_Android Sqlite_Database Update - Fatal编程技术网

Android:onUpgrade在数据库升级时未调用

Android:onUpgrade在数据库升级时未调用,android,database,sqlite,android-sqlite,database-update,Android,Database,Sqlite,Android Sqlite,Database Update,我正在开发我的应用程序的第二个版本。我正面临一个问题 在我的应用程序中,数据库位于assets文件夹中。现在我想在第二个版本中从assets文件夹更新我的数据库 我尝试将代码升级为: // Data Base Version. private static final int DATABASE_VERSION = 2; 我将版本从1更改为2 //建造师 public DataBaseHelperClass(Context myContext) { super(myContex

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

在我的应用程序中,数据库位于assets文件夹中。现在我想在第二个版本中从assets文件夹更新我的数据库

我尝试将代码升级为:

// Data Base Version.
private static final int DATABASE_VERSION = 2;
我将版本从1更改为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/";
}
//创建数据库

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().
//检查数据库

public boolean checkDataBase(){
    File databaseFile = new File(DB_PATH + DATABASE_NAME);
    return databaseFile.exists();        
}
//从资产复制数据库

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(); 
}
//开放数据库

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

@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.
}
//关于更新数据库

@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后,数据库不会更新。 现在我该怎么办

我的应用程序的第一个版本已经上市。不知道如何在第二个版本中更新数据库

请给我一些宝贵的建议,我正在申请中,不能搬家


编辑:onUpgrade未呼叫。当我试图在onUpgrade的第1行打印日志时,它没有被打印。所以问题是,onUpgrade没有打电话。

为了升级数据库,您需要通过操作模式来正确地进行操作。我发现我的问题的两个答案都很有用


此链接主要回答了这一问题:


onUpgrade()
将在
getWriteableDatabase()
getReadableDatabase()
中被调用,因此如果您从未专门打开数据库进行写入或读取,您必须手动调用
onUpgrade()
或打开数据库。

要测试您的onUpgrade日期,您应该:

@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 {

    }
}
  • 安装旧版本的应用程序。启动它,让它创建一个数据库
  • 安装应用程序的新版本。这应该通过一个特殊的adb命令来完成:
    adb install-r
    -r
    键模拟应用程序更新过程。没有
    -r
    adb install
    只会删除旧版本并安装新版本,因此不会调用onUpgrade

  • 但是你最好为此写一个测试。谷歌搜索。

    我得到了答案。我在构造函数中所做的更改仅限于:

    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/";
    }
    

    这对我来说很有用。

    如果我们通过复制方法使用exist数据库,我们必须将当前数据库版本设置为SQLITEDABASE实例。每次需要升级应用程序调用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;
    

    }

    实际上,我想删除数据库,然后再次从“资产”文件夹复制它。应用程序正在UI中更新,但数据库未更新?你能帮我一下吗?你能详细说明一下你的建议吗。因为我不知道从哪里调用onUpgrade(),也不知道该做什么。您在哪里调用createDatabase()?因为在那里你调用了getWriteableDatabase()和getReadableDatabase(),但我也不知道“this”是什么对象。这应该足以调用onUpgrade()。您做了哪些更改?“每次需要升级应用程序时,都会调用getWritableDatabase(),onUpgrade()函数。”谢谢,为我节省了很多时间。