weimingfei
3 天以前 c7376f8d1091b70597ba4430a281496b585b386e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package com.doumee.keyCabinet.dao;
 
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;
 
@Database(entities = {CabinetGridDo.class}, version = 1,exportSchema = false)
public abstract class CabinetGridDB extends RoomDatabase {
    public abstract CabinetGridDao cabinetGridDao();
    private static CabinetGridDB INSTANCE;
    private static final Object sLock = new Object();
    //数据库迁移 1-->2
    static final Migration MIGRATION_1_2 = new Migration(1, 2) {
        @Override
        public void migrate(SupportSQLiteDatabase database) {
            database.execSQL("ALTER TABLE department ADD COLUMN phone_num TEXT");
            //database.execSQL("ALTER TABLE department ADD COLUMN phone_num INTEGER NOT NULL DEFAULT 0");
        }
    };
    //跳跃式迁移
    static final Migration MIGRATION_2_4 = new Migration(2, 4) {
        @Override
        public void migrate(SupportSQLiteDatabase database) {
            //创建表
            database.execSQL(
                    "CREATE TABLE student_new (student_id TEXT, student_name TEXT, phone_num INTEGER, PRIMARY KEY(student_id))");
            //复制表
            database.execSQL(
                    "INSERT INTO student_new (student_id, student_name, phone_num) SELECT student_id, student_name, phone_num FROM student");
            //删除表
            database.execSQL("DROP TABLE student");
            //修改表名称
            database.execSQL("ALTER TABLE student_new RENAME TO students");
        }
    };
    //跳跃式迁移
    static final Migration MIGRATION_1_4 = new Migration(1, 4) {
        @Override
        public void migrate(SupportSQLiteDatabase database) {
            //创建表
            database.execSQL(
                    "CREATE TABLE student_new (student_id TEXT, student_name TEXT, phone_num INTEGER, PRIMARY KEY(student_id))");
            //复制表
            database.execSQL(
                    "INSERT INTO student_new (student_id, student_name, phone_num) SELECT student_id, student_name, phone_num FROM student");
            //删除表
            database.execSQL("DROP TABLE student");
            //修改表名称
            database.execSQL("ALTER TABLE student_new RENAME TO students");
        }
    };
 
    public static CabinetGridDB getInstance(Context context) {
        synchronized (sLock) {
            if (INSTANCE == null) {
                INSTANCE =
                        Room.databaseBuilder(context.getApplicationContext(), CabinetGridDB.class, "CabinetGrid.db")
                                .allowMainThreadQueries()//Room不允许在主线程中访问数据库
                                //.addMigrations(MIGRATION_1_2)//数据库迁移
                                //.addMigrations(MIGRATION_1_2,MIGRATION_2_4,MIGRATION_1_4)//数据库迁移 ,可以用1_4,快速迁移
                                .build();
            }
            return INSTANCE;
        }
    }
}