在Play商店中更新应用程序时如何处理SQLite数据库?

时间:2018-08-29 10:26:46

标签: android sqlite

我的SQlite数据库中有四个表,用于以前的版本。 现在我添加了新表,现在我的SQlite数据库中有五个表 在将apk上传到Play商店之前,我应该怎么办? 不会崩溃。 我知道在Upgrade()上,但是我不知道如何正确处理它, 请任何人告诉我如何处理这种情况。

3 个答案:

答案 0 :(得分:2)

在build.gradle中增加versionCode,然后从onUpgrade覆盖SQLiteOpenHelper方法并将其放入:

@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int oldVersion, int newVersion) {

    //update to v3 example considering the oldversion was 1 or 2
    if (oldVersion < 3) {
     //do your sql stuff here 
    }


}

对于直接删除新版本的设备,请不要忘记使用OnCreate方法添加新表。

希望有帮助!

答案 1 :(得分:1)

对于安装新版本的用户,将调用onCreate,并且将像其他人一样创建新表。

如果有人更新了您的应用,则会调用onUpdate。您应该在此处添加表创建代码,并注意要处理的其他版本。

cke_colorauto

这里有一个链接,其中Elye解释了SQLite数据库迁移:

https://medium.com/@elye.project/android-sqlite-database-migration-b9ad47811d34

答案 2 :(得分:-1)

这是导览DatabaseHelper的外观。

public class DatabaseHelper extends SQLiteOpenHelper {

public static final String TAG = "DatabaseHelper";
public static final String DATABASE_NAME = "mydb.db3";
private static DatabaseHelper sSingleton = null;
private final boolean mDatabaseOptimizationEnabled;

protected DatabaseHelper(Context context, String databaseName, boolean optimizationEnabled) {
    super(context, databaseName, null, DatabaseVersions.VERSION_NEW);
    mDatabaseOptimizationEnabled = optimizationEnabled;
}

public static synchronized DatabaseHelper getInstance(Context context) {
    if (sSingleton == null) {
        sSingleton = new DatabaseHelper(context, DATABASE_NAME, true);
    }
    return sSingleton;
}

@Override
public void onConfigure(SQLiteDatabase db) {
    super.onConfigure(db);

}

public SQLiteDatabase getDatabase(boolean writable) {
    return writable ? getWritableDatabase() : getReadableDatabase();
}
@Override
public void onCreate(SQLiteDatabase db) {
    createDatabase(db, true, DatabaseVersions.VERSION_OLD, DatabaseVersions.VERSION_NEW);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    Log.i(TAG, "Upgrading from version " + oldVersion + " to " + newVersion);

    switch (DatabaseVersions.VERSION_OLD) {
         case 1:
               //do here all the modifications that happened from db_version 1 to db_version 2 (alter tables, table creation, etc) notice no "break" after each case, so that if a user upgrades from 1 to 3, all the changes can happen for each sequential version.
         case 2:
               //do here all the modifications that happened from db_version 2 to db_version 3 (alter tables, table creation, etc)
    }

    createDatabase(db, false, oldVersion, newVersion);
}

private void createDatabase(SQLiteDatabase db, boolean freshDatabase, int oldVersion, int newVersion) {
    if (freshDatabase) {

        db.execSQL("CREATE TABLE " + Tables.STORIES + " (" +
                BaseColumns._ID + " INTEGER PRIMARY KEY AUTOINCREMENT," +
                Stories.NAME + " TEXT," +
                Stories.STATUS + " TEXT," +
                Stories.DESCRIPTION + " TEXT," +
                Stories.CATEGORY + " TEXT," +
                Stories.ATTACHMENTS + " TEXT," +
                Stories.CREATION_DATE + " DATETIME," +
                Stories.PRICE + " TEXT" +
                ");");
    }

    if (mDatabaseOptimizationEnabled) {
        db.execSQL("ANALYZE;");
    }
}

public interface Tables {
    public static final String STORIES = "stories";
    public static final String LOG = "log";
}