简体   繁体   English

应用随附数据库以更新版本时更新SQLite数据库

[英]Updating SQLite database when app shipped with database for releasing new version

I am shipping the database file with our android application. 我正在使用我们的android应用程序运送数据库文件。 For that I followed How to ship an Android application with a database? 为此,我遵循了如何将带有数据库的Android应用程序交付? answered by Danny Remington - OMS . Danny Remington-OMS回答。

Now I have to provide the new release In that the database schema has changed, so First I am copying the new db file from assets to data/data directory and writing a insert statement as following in the onUpgrade method of SqliteOpenHelper class: 现在,我必须提供新版本,因为数据库架构已更改,因此首先,我将新的db文件从资产复制到data / data目录,并在SqliteOpenHelper类的onUpgrade方法中编写插入语句,如下SqliteOpenHelper

//rename the old database
new File(DB_PATH + DATABASE_NAME).renameTo(new File(DB_PATH + "DB_old"));
    try 
    {
        //copting new db file from assets to data/data dir
        copyDataBase();
    } 
    catch (IOException e) 
    {
        e.printStackTrace();
    }                       
    SQLiteDatabase DB_old = SQLiteDatabase.openDatabase(DB_PATH + "DB_old", null, SQLiteDatabase.OPEN_READWRITE);
    SQLiteDatabase DB_new = SQLiteDatabase.openDatabase(DB_PATH + "DB_new", null, SQLiteDatabase.OPEN_READWRITE);
    //insert statement from old to new db
    String query = "INSERT INTO DB_new.table1(user_id,user_name) SELECT * FROM DB_old.table1";

    DB_new.execSQL(query);
    DB_old.close();
    DB_new.close();
    //deleting old db file
    new File(DB_PATH + "DB_old").delete();

But the insert statement is throwing a exception android.database.sqlite.SQLiteException: no such table: DB_new.table1 但是insert语句android.database.sqlite.SQLiteException: no such table: DB_new.table1android.database.sqlite.SQLiteException: no such table: DB_new.table1异常android.database.sqlite.SQLiteException: no such table: DB_new.table1

How I can achieve this.Please help me on this. 我该如何实现。请对此提供帮助。

In your sqlite helper, there is a method onUpgrade which triggers when the version you pass to the super's constructor (of the SqliteOpenHelper ) changes. 在您的sqlite助手中,有一个onUpgrade方法,当您传递给Super的构造函数( SqliteOpenHelper )的版本更改时,该方法会触发。 Thus, you need to change this number, so that onUpgrade fires, then put your code for "re-installing" the database inside this method. 因此,您需要更改此数字,以便触发onUpgrade ,然后将用于“重新安装”数据库的代码放入此方法中。

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

相关问题 在商店中发布新版本的Android应用程序时更新SQLite数据库 - Updating SQLite database when releasing new version of the android app on the store 更新新版本应用程序时删除sqlite数据库 - delete sqlite database when updating new version of application 发布新版本的应用程序,它将更新数据库中的特定表-android - releasing a new version of app that will update specific table in database-android Android SQLite-具有另一个应用程序版本的新数据库 - Android SQLite - new database with another app version 发布新的应用程序版本时更新 android sqlite 中某列的某些值 - Update some values of a column in android sqlite when releasing new app version 将 SQLCipher 与 android 附带的 sqlite 数据库文件一起使用 - using SQLCipher with android shipped sqlite database file SQLite数据库未使用应用程序更新进行更新 - Sqlite database not updating with app update SQLITE - 设置新数据库的版本 - SQLITE - setting the version of a new database 在我的下一个 Android 应用程序更新中使用新的数据库版本覆盖现有的 Sqlite 数据库 - Overwrite existing shipped Sqlite DB with the new DB version on my next Android App Update 在Play商店中更新应用程序时如何处理SQLite数据库? - How to deal with SQLite database when updating the app on play store?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM