Android jetpack room SQLLite 数据库 修改字段
Posted 安果移不动
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android jetpack room SQLLite 数据库 修改字段相关的知识,希望对你有一定的参考价值。
由于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语句
以上是关于Android jetpack room SQLLite 数据库 修改字段的主要内容,如果未能解决你的问题,请参考以下文章
Android Jetpack: Room | 中文教学视频
Android Jetpack Paging 3:带 Room 的 PagingSource
Android jetpack room 记录数据库升级日志
Android jetpack room+ViewModel+liveData 数据自动更新