使用Migration升级数据库
package com.tiger.room2;import android.content.Context;import androidx.annotation.NonNull;
import androidx.room.Database;
import androidx.room.Room;
import androidx.room.RoomDatabase;
import androidx.room.migration.Migration;
import androidx.sqlite.db.SupportSQLiteDatabase;//一定要写成抽象类
@Database(entities = {Student.class}, version = 3, exportSchema = false)
public abstract class MyDataBase extends RoomDatabase {private static final String DATABASE_NAME = "my_db.db";private static MyDataBase mInstance;public static MyDataBase getInstance(Context context) {if (mInstance == null) {synchronized (MyDataBase.class) {if (mInstance == null) {mInstance = Room.databaseBuilder(context.getApplicationContext(), MyDataBase.class, DATABASE_NAME).addMigrations(MIGRATION_1_2,MIGRATION_2_3)//添加版本升级
// .allowMainThreadQueries() 允许主线程操作数据库.build();}}}return mInstance;}public abstract StudentDao getStudentDao();//room自动实现static final Migration MIGRATION_1_2 = new Migration(1, 2) {@Overridepublic void migrate(@NonNull SupportSQLiteDatabase database) {database.execSQL("ALTER TABLE student ADD COLUMN sex INTEGER NOT NULL DEFAULT 1");}};static final Migration MIGRATION_2_3 = new Migration(2, 3) {@Overridepublic void migrate(@NonNull SupportSQLiteDatabase database) {database.execSQL("ALTER TABLE student ADD COLUMN barData INTEGER NOT NULL DEFAULT 1");}};}
package com.tiger.room2;import androidx.room.ColumnInfo;
import androidx.room.Entity;
import androidx.room.Ignore;
import androidx.room.PrimaryKey;@Entity(tableName = "student")
public class Student {@PrimaryKey(autoGenerate = true)//主键 自增@ColumnInfo(name = "id",typeAffinity = ColumnInfo.INTEGER)//指定列名 类型public int id;@ColumnInfo(name = "name",typeAffinity = ColumnInfo.TEXT)//指定列名 类型public String name;@ColumnInfo(name = "age",typeAffinity = ColumnInfo.INTEGER)//指定列名 类型public int age;@ColumnInfo(name = "sex",typeAffinity = ColumnInfo.INTEGER)//指定列名 类型public int sex;@ColumnInfo(name = "barData",typeAffinity = ColumnInfo.INTEGER)//指定列名 类型public int barData;// @Ignore
// public boolean flag; 属性也可以忽略 ,代表它不是数据库的一列public Student(int id, String name, int age, int sex, int barData) {this.id = id;this.name = name;this.age = age;this.sex = sex;this.barData = barData;}@Ignorepublic Student(int id, String name, int age) {this.id = id;this.name = name;this.age = age;}@Ignore //room忽略此构造方法 构造对象public Student(String name, int age) {this.name = name;this.age = age;}@Ignorepublic Student(int id) {this.id = id;}@Overridepublic String toString() {return "Student{" +"id=" + id +", name='" + name + '\'' +", age=" + age +'}';}
}
异常处理
Schema文件
在build.gradle上面加
plugins {id("com.android.application")
}android {namespace = "com.tiger.room2"compileSdk = 34defaultConfig {applicationId = "com.tiger.room2"minSdk = 28targetSdk = 34versionCode = 1versionName = "1.0"testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"javaCompileOptions {annotationProcessorOptions {argument("room.schemaLocation","$projectDir/schemas".toString())//指定数据库导出的位置}}}buildTypes {release {isMinifyEnabled = falseproguardFiles(getDefaultProguardFile("proguard-android-optimize.txt"),"proguard-rules.pro")}}compileOptions {sourceCompatibility = JavaVersion.VERSION_1_8targetCompatibility = JavaVersion.VERSION_1_8}
}dependencies {//room依赖 APIimplementation("androidx.room:room-runtime:2.6.1")//帮助我们生成一些代码 生成接口的实现类annotationProcessor("androidx.room:room-compiler:2.6.1")//适配的依赖implementation("androidx.appcompat:appcompat:1.6.1")implementation("com.google.android.material:material:1.8.0")implementation("androidx.constraintlayout:constraintlayout:2.1.4")testImplementation("junit:junit:4.13.2")androidTestImplementation("androidx.test.ext:junit:1.1.5")androidTestImplementation("androidx.test.espresso:espresso-core:3.5.1")
}
改成true
销毁和重建策略
预填充数据库
要同样的表结构,才能导进去