• Android jetpack room 数据库的升级


    如果用户设备上数据库版本为1

    而当前要安装的App数据库版本为3怎么办

    Room会先判断当前有没有直接从1到3的的升级方案,如果有,就直接执行从1到3的升级方案,如果没有,那么Room会按照顺序先后执行 Migration(1,2)、Migration(2,3)以完成升级。

    修改dataBase文件

    1. package com.anguomob.jecpack.database
    2. import android.content.Context
    3. import androidx.room.Database
    4. import androidx.room.Room
    5. import androidx.room.RoomDatabase
    6. import androidx.room.migration.Migration
    7. import androidx.sqlite.db.SupportSQLiteDatabase
    8. import com.anguomob.jecpack.bean.Student
    9. import com.anguomob.jecpack.dao.StudentDao
    10. import okhttp3.internal.Internal.instance
    11. @Database(entities = [Student::class], version = 3, exportSchema = false)
    12. abstract class MyDataBase : RoomDatabase() {
    13. companion object {
    14. var DATABASE_NAME = "my_db.db"
    15. private lateinit var instance: MyDataBase
    16. //数据库从1 到2 版本的升级
    17. var MIGATION_1_2: Migration = object : Migration(1, 2) {
    18. override fun migrate(database: SupportSQLiteDatabase) {
    19. //新增性别
    20. database.execSQL("ALTER TABLE student ADD COLUMN sex INTEGER NOT NULL DEFAULT 1")
    21. }
    22. }
    23. var MIGATION_2_3: Migration = object : Migration(2, 3) {
    24. override fun migrate(database: SupportSQLiteDatabase) {
    25. //新增性别
    26. database.execSQL("ALTER TABLE student ADD COLUMN `bar_data` INTEGER NOT NULL DEFAULT 1")
    27. }
    28. }
    29. fun getSingle(context: Context): MyDataBase {
    30. if (::instance.isInitialized.not()) {
    31. instance = Room.databaseBuilder(
    32. context.applicationContext,
    33. MyDataBase::class.java,
    34. DATABASE_NAME
    35. )
    36. // .allowMainThreadQueries()//允许主线程操作数据库
    37. .addMigrations(MIGATION_1_2, MIGATION_2_3)
    38. .build();
    39. }
    40. return instance;
    41. }
    42. }
    43. abstract fun getStudentDao(): StudentDao
    44. }

     

     数据库的版本也修改一下。

    其中呢数据bean也要跟着变化

    1. package com.anguomob.jecpack.bean
    2. import androidx.room.ColumnInfo
    3. import androidx.room.Entity
    4. import androidx.room.Ignore
    5. import androidx.room.PrimaryKey
    6. @Entity(tableName = "student")
    7. data class Student(
    8. @PrimaryKey(autoGenerate = true)
    9. @ColumnInfo(name = "id", typeAffinity = ColumnInfo.INTEGER)
    10. var id: Int,
    11. @ColumnInfo(name = "name", typeAffinity = ColumnInfo.TEXT)
    12. var name: String,
    13. @ColumnInfo(name = "age", typeAffinity = ColumnInfo.INTEGER)
    14. var age: Int,
    15. @ColumnInfo(name = "sex", typeAffinity = ColumnInfo.INTEGER)
    16. var sex: Int,
    17. @ColumnInfo(name = "bar_data", typeAffinity = ColumnInfo.INTEGER)
    18. var bar_data: Int
    19. ) {
    20. @Ignore
    21. constructor(name: String, age: Int) : this(0, name, age, 1,1)
    22. @Ignore
    23. constructor(id: Int) : this(id, "", 0, 1,1)
    24. }

    弄完后可以导出来数据库

     在data/data/包名/database路径下的三个都导出到桌面一个位置

    X:\Users\Administrator\Desktop

     然后用其他的数据库软件打开

    看数据库就存在了sex 与bar_data字段

     

  • 相关阅读:
    C++-头文件书写规范(二):头文件中的保护措施【#ifndef #define...#endif 】【防止多个源文件同时包含同一个头文件时产生的声明冲突】
    Java中的网络编程是什么?
    【linux命令讲解大全】114. 网络状态监测工具iptstate和lnstat的使用
    AR编程入门:解锁虚拟与现实交融的新世界
    基于SSM的文章管理系统源代码+数据库,基于SSM的CMS内容管理系统源代码+数据库
    CuteOneP 一款php的OneDrive多网盘挂载程序 带会员 同步等功能
    杨氏矩阵解法
    linux下PHP 环境搭建
    Java8 CompletableFuture runAsync等使用学习总结 submit() execute()等
    大厂设计师力推的14款平面图设计工具!
  • 原文地址:https://blog.csdn.net/mp624183768/article/details/124917188