• 安卓学习04


    
    

    今天主要学习了数据库的迁移。

     

    1、原entity的修改

    原先的entity可以直接修改,例如原来的为:

    package com.example.roombasic;
    ​
    import androidx.room.ColumnInfo;
    import androidx.room.Entity;
    import androidx.room.PrimaryKey;
    ​
    @Entity
    public class Word {
        @PrimaryKey(autoGenerate = true)
        private int id;
    ​
        @ColumnInfo(name = "english_word")
        private String word;
    ​
        @ColumnInfo(name = "chinese_mean")
        private String chineseMean;
    ​
        public Word(String word, String chineseMean) {
            this.word = word;
            this.chineseMean = chineseMean;
        }
    ​
        public int getId() {
            return id;
        }
    ​
        public void setId(int id) {
            this.id = id;
        }
    ​
        public String getWord() {
            return word;
        }
    ​
        public void setWord(String word) {
            this.word = word;
        }
    ​
        public String getChineseMean() {
            return chineseMean;
        }
    ​
        public void setChineseMean(String chineseMean) {
            this.chineseMean = chineseMean;
        }
    }

    可以直接修改为:

    package com.example.roombasic;
    ​
    import androidx.room.ColumnInfo;
    import androidx.room.Entity;
    import androidx.room.PrimaryKey;
    ​
    @Entity
    public class Word {
        @PrimaryKey(autoGenerate = true)
        private int id;
    ​
        @ColumnInfo(name = "english_word")
        private String word;
    ​
        @ColumnInfo(name = "chinese_mean")
        private String chineseMean;
        
        @ColumnInfo(name = "is_ok")
        private boolean isOk;
    ​
        public boolean isOk() {
            return isOk;
        }
    ​
        public void setOk(boolean ok) {
            isOk = ok;
        }
    ​
        public Word(String word, String chineseMean) {
            this.word = word;
            this.chineseMean = chineseMean;
        }
    ​
        public int getId() {
            return id;
        }
    ​
        public void setId(int id) {
            this.id = id;
        }
    ​
        public String getWord() {
            return word;
        }
    ​
        public void setWord(String word) {
            this.word = word;
        }
    ​
        public String getChineseMean() {
            return chineseMean;
        }
    ​
        public void setChineseMean(String chineseMean) {
            this.chineseMean = chineseMean;
        }
    }

    新添加的属性记得添加get和set函数。

    2、database的修改

    1、不保留原有数据的迁移

    不保留原有数据的修改,可以直接将数据库类的版本修改为原版本+1,然后构建数据库类的时候添加一个.fallbackToDestructiveMigration()。

    package com.example.roombasic;
    ​
    import android.content.Context;
    ​
    import androidx.room.Database;
    import androidx.room.Room;
    import androidx.room.RoomDatabase;
    @Database(entities = {Word.class},version = 2,exportSchema = false)
    public abstract class WordDatabase extends RoomDatabase {
        private static WordDatabase INSTANCE;
        static synchronized WordDatabase getDatabase(Context context){
            if (INSTANCE == null) {
                INSTANCE = Room.databaseBuilder(context.getApplicationContext(),WordDatabase.class,"word_database")
                        .fallbackToDestructiveMigration()
                        .build();
            }
            return INSTANCE;
        }
    ​
        public abstract WordDao getWordDao();
    }

    2、保留数据的迁移

    此时需要使用另一个方法.addMigrations(),并添加自定义的迁移方式,如:

    package com.example.roombasic;
    ​
    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 = {Word.class},version = 2,exportSchema = false)
    public abstract class WordDatabase extends RoomDatabase {
        private static WordDatabase INSTANCE;
        static synchronized WordDatabase getDatabase(Context context){
            if (INSTANCE == null) {
                INSTANCE = Room.databaseBuilder(context.getApplicationContext(),WordDatabase.class,"word_database")
                        .addMigrations(MIGRATION_1_2)
                        .build();
            }
            return INSTANCE;
        }
    ​
        public abstract WordDao getWordDao();
    ​
        private static final Migration MIGRATION_1_2 = new Migration(1,2) {
            @Override
            public void migrate(@NonNull SupportSQLiteDatabase database) {
                database.execSQL("ALTER TABLE word ADD COLUMN is_ok INTEGER NOT NULL DEFAULT 0");
            }
        };
    }

    • Migration(1,2) 中的两个参数是迁移的两个版本号

    • ALTER TABLE word ADD COLUMN is_ok INTEGER NOT NULL DEFAULT 0

      • 是增加一列,名称为is_ok,不能为空,默认值为0。

      • sqlite中没有boolean中,用int代替。

     

  • 相关阅读:
    17.1.2.1 Advantages and Disadvantages of Statement-Based and Row-Based Replication
    17.1.2 Replication Formats
    Setting the Master Configuration on the Slave
    17.1.1.9 Introducing Additional Slaves to an Existing Replication Environment
    17.1.1.8 Setting Up Replication with Existing Data
    17.1.1.7 Setting Up Replication with New Master and Slaves
    17.1.1.6 Creating a Data Snapshot Using Raw Data Files
    列出display的值,并说明它们的作用
    CSS设置DIV居中
    CSS选择符有哪些?哪些属性可以继承?优先级算法如何计算?
  • 原文地址:https://www.cnblogs.com/wuren-best/p/12260895.html
Copyright © 2020-2023  润新知