赞
踩
由于sqlLite 修改字段比较复杂。所以建议销毁、复制数据。然后重建数据
将Sex 字段由Int转换为String
修改数据bean
- package com.anguomob.jecpack.bean
-
- import androidx.room.ColumnInfo
- import androidx.room.Entity
- import androidx.room.Ignore
- import androidx.room.PrimaryKey
-
- @Entity(tableName = "student")
- data class Student(
- @PrimaryKey(autoGenerate = true)
- @ColumnInfo(name = "id", typeAffinity = ColumnInfo.INTEGER)
- var id: Int,
- @ColumnInfo(name = "name", typeAffinity = ColumnInfo.TEXT)
- var name: String,
- @ColumnInfo(name = "age", typeAffinity = ColumnInfo.INTEGER)
- var age: Int,
- /**
- * V3
- */
- // @ColumnInfo(name = "sex", typeAffinity = ColumnInfo.INTEGER)
- // var sex: Int,
- /**
- * V4
- */
- @ColumnInfo(name = "sex", typeAffinity = ColumnInfo.TEXT)
- var sex: String,
- @ColumnInfo(name = "bar_data", typeAffinity = ColumnInfo.INTEGER)
- var bar_data: Int
- ) {
-
- @Ignore
- constructor(name: String, age: Int) : this(0, name, age, "M", 1)
-
-
- @Ignore
- constructor(id: Int) : this(id, "", 0, "M", 1)
- }
-
-

具体操作
- package com.anguomob.jecpack.database
-
- import android.content.Context
- import androidx.room.Database
- import androidx.room.Room
- import androidx.room.RoomDatabase
- import androidx.room.migration.Migration
- import androidx.sqlite.db.SupportSQLiteDatabase
- import com.anguomob.jecpack.bean.Student
- import com.anguomob.jecpack.dao.StudentDao
- import okhttp3.internal.Internal.instance
-
- @Database(entities = [Student::class], version = 4, exportSchema = true)
- abstract class MyDataBase : RoomDatabase() {
- companion object {
- var DATABASE_NAME = "my_db.db"
- private lateinit var instance: MyDataBase
-
- //数据库从1 到2 版本的升级
- var MIGATION_1_2: Migration = object : Migration(1, 2) {
- override fun migrate(database: SupportSQLiteDatabase) {
- //新增性别
- database.execSQL("ALTER TABLE student ADD COLUMN sex INTEGER NOT NULL DEFAULT 1")
- }
- }
-
- var MIGATION_2_3: Migration = object : Migration(2, 3) {
- override fun migrate(database: SupportSQLiteDatabase) {
- //新增性别
- database.execSQL("ALTER TABLE student ADD COLUMN `bar_data` INTEGER NOT NULL DEFAULT 1")
- }
- }
-
-
- var MIGATION_3_4: Migration = object : Migration(3, 4) {
- override fun migrate(database: SupportSQLiteDatabase) {
- //修改性别字段为Text
- // 1、 创建临时表
- database.execSQL(
- "CREATE TABLE `temp_student` ("
- + "`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "
- + "`name` TEXT NOT NULL, "
- + "`age` INTEGER NOT NULL, "
- + "`sex` TEXT NOT NULL DEFAULT 'M',"
- + " `bar_data` INTEGER NOT NULL"
- + ")"
- )
-
- //2 复制数据
- database.execSQL(
- "INSERT INTO `temp_student` (name,age,sex,bar_data)"
- + "SELECT name,age,sex,bar_data FROM student"
- )
-
-
- //3 删除原来表
- database.execSQL("DROP TABLE STUDENT")
- //4 重命名
- database.execSQL("ALTER TABLE temp_student RENAME TO student")
- //修改原来的表的数据为正常数据
- database.execSQL(
- "update student set sex = 'M' where sex = '1'"
- )
-
- database.execSQL(
- "update student set sex = 'F' where sex = '2'"
- )
-
- }
- }
-
-
- fun getSingle(context: Context): MyDataBase {
- if (::instance.isInitialized.not()) {
- instance = Room.databaseBuilder(
- context.applicationContext,
- MyDataBase::class.java,
- DATABASE_NAME
- )
- // .allowMainThreadQueries()//允许主线程操作数据库
- .addMigrations(MIGATION_1_2, MIGATION_2_3, MIGATION_3_4)
- //出现异常问题 重建数据表,同时数据也会丢失。
- .fallbackToDestructiveMigration()
- .build();
- }
-
- return instance;
- }
-
-
- }
-
-
- abstract fun getStudentDao(): StudentDao
-
-
- }

重点关注代码 MIGATION_3_4里面的sql语句
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。