使用值和as 定义新属性 newAttribute 的迁移。
DEFAULT``NOT NULL
码
database.execSQL("ALTER TABLE tableName ADD COLUMN newAttribute INTEGERDEFAULT 0 NOT NULL")
完整代码
@Database(entities = arrayOf(ModelName::class), version = 2)@TypeConverters(Converters::class)abstract class DatabaseName : RoomDatabase() { abstract fun daoname(): Daoname companion object { private var INSTANCE: DatabaseName? = null fun getAppDatabase(context: Context): DatabaseName { if (INSTANCE == null) { INSTANCE = Room.databaseBuilder(context.applicationContext, DatabaseName::class.java, DATAbase_NAME) .addMigrations(MIGRATION_1_2) .build() } return INSTANCE as DatabaseName } val MIGRATION_1_2: Migration = object : Migration(1, 2) { override fun migrate(database: SupportSQLiteDatabase) { database.execSQL("ALTER TABLE tableName ADD COLUMN newAttribute INTEGER DEFAULT 0 NOT NULL") } } }}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)