SQLiteOpenHelper有两个需要实现的方法。
- public void onCreate(SQLiteDatabase db)
- public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
简单的记录一下这两个方法在什么时候会被调用。
onCreate方法是在手机安装你的App的时候调用。就是说手机第一次安装或者是卸载了,再重新安装的时候被调用。
onUpgrade是在构造方法 public DataBaseHelp(Context context, String name, CursorFactory factory, int version)中的version的值变大的时候,会被回调。
所以在使用的时候。
- 如果项目已经上线过了,之前没用过SQLite,现在业务拓展需要加SQLite,那么在onCreate方法里建表就好了。
- 如果之前上线过,之前也用了SQLite,但是现在需要新加表,那么需要在onCreate中有所有表的创建方法,在onUpgrade需要有新表的创建方法。同时记得把version版本升高。