如何迁移 Room 数据库并添加一个日期列?

huangapple go评论113阅读模式
英文:

How to migrate a Room database adding a Date column?

问题

I have a simple database, with one table (id, title, and description), I want to add a column to store a due date. I use the info I found in Developer Android migration and TypeConverters to migrate it, I can install to test, but it doesn't open the app.

My entity:

@Entity(tableName = "todo_db")
public class todoEnt {
    @PrimaryKey(autoGenerate = true)
    public int id;

    @ColumnInfo(name = "title")
    public String todoTitle;

    @ColumnInfo(name = "description")
    public String todoDescription;

    @ColumnInfo(name = "dueDate")
    public Date dueDate;
}

I have a Typeconverter:

public class Convertors {
    @TypeConverter
    public static Date fromTimeStamp(Long value){
        return value == null ? null : new Date(value);
    }

    @TypeConverter
    public static Long dateToTimestamp(Date date){
        if (date == null) {
            return null;
        } else {
            return date.getTime();
        }
    }
}

And I added this to my database file:

@TypeConverters({Convertors.class})

Migration code:

static final Migration M_1_2 = new Migration(1, 2) {
    @Override
    public void migrate(@NonNull SupportSQLiteDatabase database) {
        database.execSQL("ALTER TABLE todo_db ADD COLUMN dueDate INTEGER");
    }
};

Database instance creation:

INSTANCE = Room.databaseBuilder(
    context.getApplicationContext(),
    TDAHdb.class,
    "tdah_database")
    .addMigrations(M_1_2)
    .allowMainThreadQueries()
    .build();
英文:

I have a simple database, with one table (id, title and description), I want to add a column to storage a duedate. I use the info I found in Developer android migration and TypeConverters to migrate it, I can install to test, but it doesn't open the app.
I'll apreciate any help!

My entity


@Entity(tableName = "todo_db")
public class todoEnt {
    @PrimaryKey(autoGenerate = true)
    public int id;

    @ColumnInfo(name = "title")
    public String todoTitle;

    @ColumnInfo(name = "description")
    public String todoDescription;

    @ColumnInfo(name = "dueDate")
    public Date dueDate;

}

I have a Typeconverter

public class Convertors {
    @TypeConverter
    public static Date fromTimeStamp(Long value){
        return value == null ? null : new Date(value);
    }

    @TypeConverter
    public static Long dateToTimestamp(Date date){
        if (date == null) {
            return null;
        } else {
            return date.getTime();
        }
    }
}

And I added this to my database file

@TypeConverters({Convertors.class})
    static final Migration M_1_2 = new Migration(1, 2) {
        @Override
        public void migrate(@NonNull SupportSQLiteDatabase database) {
            database.execSQL("ALTER TABLE todo_db ADD duedate INTEGER");
        }
    };
   INSTANCE = Room.databaseBuilder(
       context.getApplicationContext(),
       TDAHdb.class,
       "tdah_database")
       .addMigrations(M_1_2)
       .allowMainThreadQueries()
       .build();

答案1

得分: 1

使用Room迁移时,表格和列名区分大小写。将迁移中的 duedate 更改为与实体定义中指定的 @ColumnInfo 名称相匹配的大小写,即 dueDate

database.execSQL("ALTER TABLE todo_db ADD dueDate INTEGER");
英文:

With Room migrations, table and column names are case-sensitive. Change duedate in your migration to match the case of the @ColumnInfo name specified in the entity definition, dueDate:

database.execSQL("ALTER TABLE todo_db ADD dueDate INTEGER");

huangapple
  • 本文由 发表于 2020年7月30日 06:31:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/63163412.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定