英文:
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");
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论