设置一个自增长ID列为主键:
dao.addIdProperty().primaryKey().autoincrement();
dao.addStringProperty("infoType").notNull();//非null字段
注:DevOpenHelper升级时默认会删除所有的表,里面的数据也会被删除
所以要自己去封装一下,不然升级时数据会丢失
/** WARNING: Drops all table on Upgrade! Use only during development. */
public static class DevOpenHelper extends OpenHelper {
public DevOpenHelper(Context context, String name, CursorFactory factory) {
super(context, name, factory);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.i("greenDAO", "Upgrading schema from version " + oldVersion + " to " + newVersion + " by dropping all tables");
dropAllTables(db, true);
onCreate(db);
}
}
封装,即重写升级方法
eg:
public class THDevOpenHelper extends DaoMaster.OpenHelper {
public THDevOpenHelper(Context context, String name, SQLiteDatabase.CursorFactory factory) {
super(context, name, factory);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
switch (oldVersion) {
case 1:
//创建新表,注意createTable()是静态方法
// SchoolDao.createTable(db, true);
// 加入新字段
// db.execSQL("ALTER TABLE 'moments' ADD 'audio_path' TEXT;");
// TODO
break;
}
}
}