我是 Android 和数据库的新手,我在 DatabaseIncomeOutcome.java 类中有这个方法:
public DbHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL("CREATE TABLE IF NOT EXISTS " + DATABASE_TABLE + "(" +
KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
KEY_IO + " TEXT NOT NULL, " +
KEY_NOTE + " TEXT NOT NULL);"
);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
onCreate(db);
}
}
public DatabaseIncomeOutcome(Context c){
ourContext = c;
}
public DatabaseIncomeOutcome open(){
ourHelper = new DbHelper(ourContext);
ourDatabase = ourHelper.getWritableDatabase();
return this;
当我从另一个类调用该方法时
DatabaseIncomeOutcome entry = new DatabaseIncomeOutcome(this);
entry.open();
如何以及何时onUpgrade
调用?每次我执行时都会调用它entry.open()
吗?
我也想添加这个脚本
if (newVersion > oldVersion) {
db.execSQL("ALTER TABLE foo ADD COLUMN new_column INTEGER DEFAULT 0");
上onUpgrade
添加新列,
然后,我应该如何调用“onUpgrade”,这样我就可以添加新列,如果我可能需要它以用于未来的开发。
谢谢...