Android的SQLite数据库,为什么删除表,并重新创建升级 [英] Android SQLite Database, WHY drop table and recreate on upgrade
问题描述
在教程中我下面,有很多更多的地方我看到这一点, onUpgrade - >降表,如果存在,则重新创建表
这样做的目的是什么?
私有静态类DbHelper扩展SQLiteOpenHelper {
公共DbHelper(上下文的背景下){
超(背景下,DATABASE_NAME,空,DATABASE_VERSION);
}
@覆盖
公共无效的onCreate(SQLiteDatabase DB){
db.execSQL(CREATE TABLE+ DATABASE_TABLE +(+
KEY_ROWID +INTEGER PRIMARY KEY AUTOINCREMENT,+
KEY_NAME +TEXT NOT NULL,+
KEY_HOTNESS +TEXT NOT NULL);
);
}
@覆盖
公共无效onUpgrade(SQLiteDatabase分贝,INT oldVersion,诠释静态网页){
db.execSQL(DROP TABLE IF EXISTS+ DATABASE_TABLE);
的onCreate(DB);
}
}
当你升级你应该将列添加或添加表到您的数据库,我同意。大多数onupgrade样品的实际吮吸,因为我为什么要删除所有这些数据,然后重新创建表?我发现这个博客中我把它的亚当斯增量更新方法的。它还处理,用户可能不会每次版本升级您的应用程序的情况。
这里是一个很好的博客上sqlite的onupgrade那不删除表。
In the tutorials I am following and a lot of more places I see this, onUpgrade -> drop table if exists, then recreate table.
What is the purpose of this?
private static class DbHelper extends SQLiteOpenHelper{
public DbHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" +
KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
KEY_NAME + " TEXT NOT NULL, " +
KEY_HOTNESS + " TEXT NOT NULL);"
);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
onCreate(db);
}
}
I agree when you upgrade you should be adding columns or adding tables to your database. Most of the onupgrade samples actually suck because why am I deleting all this data then recreating the table? I found this blog entry I call it the Adams Incremental Update Method. It also handles situations where users may have not upgraded your app with each release.
Here is a good blog on sqlite onupgrade that doesn't do drop table.
这篇关于Android的SQLite数据库,为什么删除表,并重新创建升级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!