java.lang.IllegalStateException:迁移未正确处理表 [英] java.lang.IllegalStateException: Migration didn't properly handle table

查看:1291
本文介绍了java.lang.IllegalStateException:迁移未正确处理表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将一个空字段类型迁移到Room中的文本?

How do you migrate an empty field type to text in Room ?

现在我正面临这个问题:

Right now I'm facing this issue :

java.lang.IllegalStateException:迁移未正确处理data_table

java.lang.IllegalStateException: Migration didn't properly handle data_table

预期:TableInfo {name ='data_table',columns = url = Column {name ='url', type ='TEXT',notNull = false, primaryKeyPosition = 0} .....

Expected: TableInfo{name='data_table', columns= url=Column{name='url', type='TEXT', notNull=false, primaryKeyPosition=0}.....

找到:TableInfo {name ='data_table',columns = url = Column {name ='url', type ='',notNull = false,primaryKeyPosition = 0} .....

Found: TableInfo{name='data_table', columns= url=Column{name='url', type='', notNull=false, primaryKeyPosition=0}.....

我尝试使用UNDEFINED typeAffinity,但这没有效果.

I've tried using the UNDEFINED typeAffinity, but that has no effect.

推荐答案

问题是创建表时,创建者未明确指示该列. 有两种方法可以解决问题.

The problem is when creating the table, the creator did't indicate the column explicitly. There are two ways to be chosen to solve the problem.

  1. 您可以在迁移期间创建一个新的数据库,并将所有旧数据复制到新的数据库中.这样.

  1. You can create a new DB during migration and copy all the old data into the new one. Like this.

static final Migration MIGRATION_1_2 = new Migration(1, 2) {
    @Override
    public void migrate(SupportSQLiteDatabase database) {
        // Create the new table
        database.execSQL(
                "CREATE TABLE data_table_new (url TEXT");
        // Copy the data
        database.execSQL(
                "INSERT INTO data_table_new url SELECT url FROM data_table");
        // Remove the old table
        database.execSQL("DROP TABLE data_table");
        // Change the table name to the correct one
        database.execSQL("ALTER TABLE data_table_new RENAME TO data_table");
    }
};

但是在使用大数据库时效率低下.所以我用第二种方式.

But it is inefficient when working with large db. So I use the second way.

2您可以使用自己喜欢的数据库管理器或终端将表迁移为具有显式列类型的新表.然后将新的数据库放入您的项目中.

2 You migrate the table to new table with explicit column type with your favourite db manager or terminal. Then put the new db into your project.

这篇关于java.lang.IllegalStateException:迁移未正确处理表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆