且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

更新时清除数据

更新时间:2023-12-05 13:51:58

每当需要升级数据库时(例如,当用户下载新版本的应用程序时),数据库版本号的变化会调用 onUpgrade() 方法,您可以覆盖 SQLiteOpenHelper 类提供的这个方法:

Whenever a database needs to be upgraded(when a user downloads a new version of an app, for example), the change in the database version number calls the onUpgrade() method, you can override thiis method provided with SQLiteOpenHelper class:

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    Log.w("TaskDBAdapter", "Upgrading from version "+oldVersion
            +" to "+newVersion
            +", which will destroy all old data");
    for (String string : table_names)
            db.execSQL("drop table if exists "+ string);
    onCreate(db);
}