@Entity(tableName = "text_table")
data class TextEntity (
@PrimaryKey(autoGenerate = true)
@ColumnInfo(name = "id")
var id : Int,
@ColumnInfo(name = "text")
var text : String,
@ColumnInfo(name = "currentDate")
var currentDate : Date
// @Embedded
// var testDataset : testDataSet
)
class DateConverters{
@TypeConverter
fun fromTimestamp(value: Long): Date {
return Date(value)
}
@TypeConverter
fun dateToTimestamp(date: Date): Long {
return date.time
}
}
@Database(
entities = [TextEntity::class],
version = 33,
exportSchema = false
)
@TypeConverters(DateConverters::class)
abstract class TextDatabase : RoomDatabase() {
abstract fun textDao() : TextDao
companion object {
@Volatile
private var INSTANCE : TextDatabase? = null
fun getDatabase(
context : Context
) : TextDatabase {
return INSTANCE ?: synchronized(this){
val instance = Room.databaseBuilder(
context.applicationContext,
TextDatabase::class.java,
"text_database"
)
.fallbackToDestructiveMigration()
// .addMigrations(MIGRATION_1_2)
.build()
INSTANCE = instance
instance
}
}
val MIGRATION_1_2 = object : Migration(26, 27) {
override fun migrate(database: SupportSQLiteDatabase) {
database.execSQL("CREATE TABLE `text_table3` (`id3` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `text3` TEXT NOT NULL)")
// database.execSQL("DROP TABLE `text_table3`")
}
}
}
}
- 참조
https://developer.android.com/training/data-storage/room/referencing-data?hl=ko
'Android Jetpack' 카테고리의 다른 글
dataStore - 1 (0) | 2022.08.06 |
---|---|
BindingAdapter + databinding (0) | 2022.08.05 |
ROOM Embeded (0) | 2022.07.22 |
ROOM Migration (0) | 2022.07.22 |
ROOM + FLOW CRUD - 4 (DELTE, UPDATE) (0) | 2022.07.17 |