列类型布尔的房间数据库迁移问题
Room database migration issue with column type Boolean
我用很少的 table 就在我的应用程序中实现了 Room 数据库。现在我必须在我的数据库中添加另一个 table 因为需求已经更新。我面临的问题是,当我执行带有列类型 boolean 的代码时,App 崩溃了。
我的实体class模型如下
@Entity(tableName = Constants.TABLE_NAME_CONVERSATION)
public class ConversationModel {
@PrimaryKey(autoGenerate = true)
private int id;
String inputWord, translatedWord,origin,targetLangCode;
boolean isSpeaking;
public ConversationModel() {
}
}
我写的迁移代码是:
private static final Migration MIGRATION_1_2 = new Migration(1, 2) {
@Override
public void migrate(SupportSQLiteDatabase database) {
String createTable = "CREATE TABLE IF NOT EXISTS 'conversation' (id INTEGER NOT NULL PRIMARY KEY, inputWord TEXT, translatedWord TEXT, origin TEXT, targetLangCode TEXT, isSpeaking BOOLEAN)";
database.execSQL(createTable);
}
};
我收到的崩溃日志是这样的:
Caused by: java.lang.IllegalStateException: Migration didn't properly handle conversation(com.translateall.language.free.translator.dictionary.speechtext.learnenglish.models.ConversationModel).
Expected:
TableInfo{name='conversation', columns={isSpeaking=Column{name='isSpeaking',type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=0}}, foreignKeys=[], indices=[]}
Found:
2019-10-29 13:24:31.433 20525-20525/com.translateall.language.free.translator.dictionary.speechtext.learnenglish E/AndroidRuntime:TableInfo{name='conversation', columns={
isSpeaking=Column{name='isSpeaking', type='BOOLEAN', affinity='1', notNull=false, primaryKeyPosition=0}}, foreignKeys=[], indices=[]}
我的模型 class 有布尔类型的项目。为什么需要整数类型?
ROOM 只允许 TEXT、INTEGER、REAL 或 BLOB 列类型。
您需要使用 isSpeaking INTEGER
。这将适用于 boolean isSpeaking;
所以你应该使用:-
String createTable = "CREATE TABLE IF NOT EXISTS 'conversation' (id INTEGER NOT NULL PRIMARY KEY, inputWord TEXT, translatedWord TEXT, origin TEXT, targetLangCode TEXT, isSpeaking INTEGER)";
P.S。获得正确 SQL 的一种方法是在更改实体后进行编译,然后查看生成的 java 的 ??????_impl java 为数据库 class(其中 ???? 是 class)创建 SQL 将在 createAllTables 方法中。
我用很少的 table 就在我的应用程序中实现了 Room 数据库。现在我必须在我的数据库中添加另一个 table 因为需求已经更新。我面临的问题是,当我执行带有列类型 boolean 的代码时,App 崩溃了。 我的实体class模型如下
@Entity(tableName = Constants.TABLE_NAME_CONVERSATION)
public class ConversationModel {
@PrimaryKey(autoGenerate = true)
private int id;
String inputWord, translatedWord,origin,targetLangCode;
boolean isSpeaking;
public ConversationModel() {
}
}
我写的迁移代码是:
private static final Migration MIGRATION_1_2 = new Migration(1, 2) {
@Override
public void migrate(SupportSQLiteDatabase database) {
String createTable = "CREATE TABLE IF NOT EXISTS 'conversation' (id INTEGER NOT NULL PRIMARY KEY, inputWord TEXT, translatedWord TEXT, origin TEXT, targetLangCode TEXT, isSpeaking BOOLEAN)";
database.execSQL(createTable);
}
};
我收到的崩溃日志是这样的:
Caused by: java.lang.IllegalStateException: Migration didn't properly handle conversation(com.translateall.language.free.translator.dictionary.speechtext.learnenglish.models.ConversationModel).
Expected:
TableInfo{name='conversation', columns={isSpeaking=Column{name='isSpeaking',type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=0}}, foreignKeys=[], indices=[]}
Found:
2019-10-29 13:24:31.433 20525-20525/com.translateall.language.free.translator.dictionary.speechtext.learnenglish E/AndroidRuntime:TableInfo{name='conversation', columns={
isSpeaking=Column{name='isSpeaking', type='BOOLEAN', affinity='1', notNull=false, primaryKeyPosition=0}}, foreignKeys=[], indices=[]}
我的模型 class 有布尔类型的项目。为什么需要整数类型?
ROOM 只允许 TEXT、INTEGER、REAL 或 BLOB 列类型。
您需要使用 isSpeaking INTEGER
。这将适用于 boolean isSpeaking;
所以你应该使用:-
String createTable = "CREATE TABLE IF NOT EXISTS 'conversation' (id INTEGER NOT NULL PRIMARY KEY, inputWord TEXT, translatedWord TEXT, origin TEXT, targetLangCode TEXT, isSpeaking INTEGER)";
P.S。获得正确 SQL 的一种方法是在更改实体后进行编译,然后查看生成的 java 的 ??????_impl java 为数据库 class(其中 ???? 是 class)创建 SQL 将在 createAllTables 方法中。