45 lines
1.6 KiB
Java
45 lines
1.6 KiB
Java
|
package com.example.myapplication.DataBase;
|
|||
|
|
|||
|
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;
|
|||
|
|
|||
|
import com.example.myapplication.model.AudioEntity;
|
|||
|
import com.example.myapplication.model.ImageEntity;
|
|||
|
import com.example.myapplication.model.Turbine;
|
|||
|
|
|||
|
@Database(entities = {ImageEntity.class, Turbine.class, AudioEntity.class}, version = 4) // 版本号增加,添加新实体
|
|||
|
public abstract class AppDatabase extends RoomDatabase {
|
|||
|
public abstract ImageDao imageDao();
|
|||
|
public abstract TurbineDao turbineDao(); // 添加新的DAO
|
|||
|
public abstract AudioDao AudioDao();
|
|||
|
private static volatile AppDatabase INSTANCE;
|
|||
|
|
|||
|
|
|||
|
|
|||
|
private static final Migration MIGRATION_4_5 = new Migration(4, 5) {
|
|||
|
@Override
|
|||
|
public void migrate(@NonNull SupportSQLiteDatabase database) {
|
|||
|
|
|||
|
}
|
|||
|
};
|
|||
|
public static AppDatabase getDatabase(Context context) {
|
|||
|
if (INSTANCE == null) {
|
|||
|
synchronized (AppDatabase.class) {
|
|||
|
if (INSTANCE == null) {
|
|||
|
INSTANCE = Room.databaseBuilder(context.getApplicationContext(),
|
|||
|
AppDatabase.class, "app_database")
|
|||
|
.fallbackToDestructiveMigration() // 简单处理,正式项目应该实现Migration
|
|||
|
.build();
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
return INSTANCE;
|
|||
|
}
|
|||
|
|
|||
|
}
|