简体   繁体   English

列类型 Boolean 的房间数据库迁移问题

[英]Room database migration issue with column type Boolean

I have implemented the Room database in my Application with few tables.我已经在我的应用程序中用几个表实现了 Room 数据库。 Now I have to add another table in my database as the requirement has been updated.现在我必须在我的数据库中添加另一个表,因为需求已经更新。 The problem I am facing is that when I execute the code with column type boolean App crashes.我面临的问题是,当我执行列类型为 boolean 的代码时,应用程序崩溃。 My entity class model is as below我的实体 class model 如下

@Entity(tableName = Constants.TABLE_NAME_CONVERSATION)
public class ConversationModel {

@PrimaryKey(autoGenerate = true)
private int id;

String inputWord, translatedWord,origin,targetLangCode;
boolean isSpeaking;


public ConversationModel() {
}
}

The migration code I have written is:我写的迁移代码是:

 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);
    }
};

And the crash log I am receiving is this:我收到的崩溃日志是这样的:

 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=[]}

My model class has boolean type item.我的 model class 有 boolean 类型的项目。 Why it is expecting integer type?为什么期待 integer 类型?

ROOM only allows column types of TEXT, INTEGER, REAL or BLOB. ROOM 仅允许 TEXT、INTEGER、REAL 或 BLOB 的列类型。

You need to use isSpeaking INTEGER .您需要使用isSpeaking INTEGER This will work with boolean isSpeaking;这将适用于boolean isSpeaking;

So you should use:-所以你应该使用: -

String createTable = "CREATE TABLE IF NOT EXISTS 'conversation' (id  INTEGER NOT NULL PRIMARY KEY, inputWord TEXT, translatedWord TEXT, origin TEXT, targetLangCode TEXT, isSpeaking INTEGER)";

PS a means of getting the correct SQL is to compile, after changing the entity, and to then look at the generated java of the??????_impl java for the database class (where???? is the class) the create SQL will be in the createAllTables method. PS a means of getting the correct SQL is to compile, after changing the entity, and to then look at the generated java of the??????_impl java for the database class (where???? is the class) the create SQL 将在createAllTables方法中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM