发布将更新database-android中特定表的新版app

时间:2017-02-20 12:26:45

标签: android database

我在谷歌游戏市场上发布了一个应用程序。其中我使用SQLassethelper库进行数据库连接。 实际上我的应用程序是一个教程应用其中还有一个表格,用于存储用户标记的最喜欢的问题。 现在我想更新问题表, 但我的问题是,如果我发布我的应用程序的新版本与更新的问题表,最喜欢的问题的表将删除?对? 那我怎么能发布我的应用程序的新版本,它不会删除用户最喜欢的表数据,但只会更新该数据库中的特定表? 我的问题有什么解决方案吗?

提前完成。

2 个答案:

答案 0 :(得分:1)

您需要注意您的用户数据,onUpgrade()类中有SQliteOpenHelper,您可以在其中匹配database version,如果您的数据库版本更多,则您之前的版本然后在onUpgrade() fucntion中创建新表格。如果您想要将新数据添加到现有表格,那么您需要编写代码来添加新行或通过inertupdate进行更新查询。 您还可以通过alter查询向现有表添加新列。

请找到有关版本upgarde的更多信息。

https://riggaroo.co.za/android-sqlite-database-use-onupgrade-correctly/

如果您在Asset中为初始数据添加了数据库文件那么您需要更改数据库pragma版本,这只是我们可以在SQliteOpenHelper类中传递的数据库版本。

见下面的onUpgrade()

示例

在我们的onUpgrade中,我们定义了以下内容:

 @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        Log.e(TAG, "Updating table from " + oldVersion + " to " + newVersion);

        //Added new column to book table - book rating 
        if (oldVersion < 2){
            db.execSQL(DROP + BookEntry.TABLE_NAME);
            db.execSQL(BookEntry.SQL_CREATE_BOOK_ENTRY_TABLE);
        }
        //Rename table to book_information - this is where things will start failing.
        if (oldVersion < 3){
            db.execSQL(DROP + BookEntry.TABLE_NAME);
            db.execSQL(BookEntry.SQL_CREATE_BOOK_ENTRY_TABLE);
        }
        // Add new column for a calculated value. By this time, if I am upgrading from version 2 to 
        // version 4, my table would already contain the new column I am trying to add below, 
        // which would result in a SQLException. These situations are sometimes difficult to spot, 
        // as you basically need to test from every different version of database to upgrade from. 
        // Some upgrades might work and some might fail with this method.
        // It is best to follow the other method that is on the master branch of this repo.
        if (oldVersion < 4){
            db.execSQL("ALTER TABLE " + BookEntry.TABLE_NAME  + " ADD COLUMN calculated_pages_times_rating INTEGER;");
        }
        //As you can probably imagine, this is a terrible way to do upgrades, Please DONT DO IT!!!!

     }


       @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            Log.e(TAG, "Updating table from " + oldVersion + " to " + newVersion);

            //Added new column to book table - book rating 
            if (oldVersion < 2){
                db.execSQL(DROP + BookEntry.TABLE_NAME);
                db.execSQL(BookEntry.SQL_CREATE_BOOK_ENTRY_TABLE);
            }
            //Rename table to book_information - this is where things will start failing.
            if (oldVersion < 3){
                db.execSQL(DROP + BookEntry.TABLE_NAME);
                db.execSQL(BookEntry.SQL_CREATE_BOOK_ENTRY_TABLE);
            }
            // Add new column for a calculated value. By this time, if I am upgrading from version 2 to 
            // version 4, my table would already contain the new column I am trying to add below, 
            // which would result in a SQLException. These situations are sometimes difficult to spot, 
            // as you basically need to test from every different version of database to upgrade from. 
            // Some upgrades might work and some might fail with this method.
            // It is best to follow the other method that is on the master branch of this repo.
            if (oldVersion < 4){
                db.execSQL("ALTER TABLE " + BookEntry.TABLE_NAME  + " ADD COLUMN calculated_pages_times_rating INTEGER;");
            }
            //As you can probably imagine, this is a terrible way to do upgrades, Please DONT DO IT!!!!


        }

答案 1 :(得分:1)

在SQLiteOpenHelper类中更改数据库版本时     void onUpgrade(SQLiteDatabase db,                 int oldVersion,                 int newVersion) 将为具有旧数据库版本的用户调用。 您应该在此处修改数据库,以包括在新数据库版本中添加的修改。 如果从此方法中删除drop table命令,则不会删除现有用户数据