在我正在关注的教程以及我看到的更多地方,onUpgrade -> 如果存在则删除表,然后重新创建表.
In the tutorials I am following and a lot of more places I see this, onUpgrade -> drop table if exists, then recreate table.
这样做的目的是什么?
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);
}
}
我同意,当您升级时,您应该在数据库中添加列或添加表.大多数 onupgrade 样本实际上都很糟糕,因为为什么我要删除所有这些数据然后重新创建表?我发现这篇博文我称之为Adams 增量更新方法.它还可以处理用户可能没有在每个版本中升级您的应用的情况.
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.
这是一个关于 sqlite onupgrade 的不错的博客,它不会删除表.
这篇关于Android SQLite 数据库,为什么要删除表并在升级时重新创建的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!