当前位置:   article > 正文

Android jetpack room SQLLite 数据库 修改字段_android room修改字段类型

android room修改字段类型

由于sqlLite 修改字段比较复杂。所以建议销毁、复制数据。然后重建数据

将Sex 字段由Int转换为String 

修改数据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. /**
  16. * V3
  17. */
  18. // @ColumnInfo(name = "sex", typeAffinity = ColumnInfo.INTEGER)
  19. // var sex: Int,
  20. /**
  21. * V4
  22. */
  23. @ColumnInfo(name = "sex", typeAffinity = ColumnInfo.TEXT)
  24. var sex: String,
  25. @ColumnInfo(name = "bar_data", typeAffinity = ColumnInfo.INTEGER)
  26. var bar_data: Int
  27. ) {
  28. @Ignore
  29. constructor(name: String, age: Int) : this(0, name, age, "M", 1)
  30. @Ignore
  31. constructor(id: Int) : this(id, "", 0, "M", 1)
  32. }

具体操作

  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 = 4, exportSchema = true)
  12. abstract class MyDataBase : RoomDatabase() {
  13. companion object {
  14. var DATABASE_NAME = "my_db.db"
  15. private lateinit var instance: MyDataBase
  16. //数据库从12 版本的升级
  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. var MIGATION_3_4: Migration = object : Migration(3, 4) {
  30. override fun migrate(database: SupportSQLiteDatabase) {
  31. //修改性别字段为Text
  32. // 1、 创建临时表
  33. database.execSQL(
  34. "CREATE TABLE `temp_student` ("
  35. + "`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "
  36. + "`name` TEXT NOT NULL, "
  37. + "`age` INTEGER NOT NULL, "
  38. + "`sex` TEXT NOT NULL DEFAULT 'M',"
  39. + " `bar_data` INTEGER NOT NULL"
  40. + ")"
  41. )
  42. //2 复制数据
  43. database.execSQL(
  44. "INSERT INTO `temp_student` (name,age,sex,bar_data)"
  45. + "SELECT name,age,sex,bar_data FROM student"
  46. )
  47. //3 删除原来表
  48. database.execSQL("DROP TABLE STUDENT")
  49. //4 重命名
  50. database.execSQL("ALTER TABLE temp_student RENAME TO student")
  51. //修改原来的表的数据为正常数据
  52. database.execSQL(
  53. "update student set sex = 'M' where sex = '1'"
  54. )
  55. database.execSQL(
  56. "update student set sex = 'F' where sex = '2'"
  57. )
  58. }
  59. }
  60. fun getSingle(context: Context): MyDataBase {
  61. if (::instance.isInitialized.not()) {
  62. instance = Room.databaseBuilder(
  63. context.applicationContext,
  64. MyDataBase::class.java,
  65. DATABASE_NAME
  66. )
  67. // .allowMainThreadQueries()//允许主线程操作数据库
  68. .addMigrations(MIGATION_1_2, MIGATION_2_3, MIGATION_3_4)
  69. //出现异常问题 重建数据表,同时数据也会丢失。
  70. .fallbackToDestructiveMigration()
  71. .build();
  72. }
  73. return instance;
  74. }
  75. }
  76. abstract fun getStudentDao(): StudentDao
  77. }

 重点关注代码 MIGATION_3_4里面的sql语句

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/477715
推荐阅读
相关标签
  

闽ICP备14008679号