Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/github/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 房间,如何设置';字段';s notnull值';这是假的吗?_Android_Android Room - Fatal编程技术网

Android 房间,如何设置';字段';s notnull值';这是假的吗?

Android 房间,如何设置';字段';s notnull值';这是假的吗?,android,android-room,Android,Android Room,我想使用room在数据库中添加一个新列。我使用Room的迁移将数据库迁移到新版本,但它不能正常工作。整数字段的默认notnull值为true,这对我来说真是个麻烦。请帮帮我。我已经在里面呆了好几个小时了 我有一门地理课 package com.example.phamf.javamvvmapp.Model; import ... @Entity (tableName = "location") public class Location { @PrimaryKey(autoGener

我想使用room在数据库中添加一个新列。我使用Room的迁移将数据库迁移到新版本,但它不能正常工作。整数字段的默认notnull值为true,这对我来说真是个麻烦。请帮帮我。我已经在里面呆了好几个小时了

我有一门地理课

package com.example.phamf.javamvvmapp.Model;
import ...

@Entity (tableName = "location")
public class Location {

    @PrimaryKey(autoGenerate = true)
    private int id;

    private float x;

    private float y;

    private String name;


    public Location(int id, float x, float y, String name) {
        this.id = id;
        this.x = x;
        this.y = y;
        this.name = name;
    }

    public Location () {

    }

    // Getter and setter
}
我想添加一个名为type的新字段,它的类型是INTEGER,所以我修改了上面的Location.java文件,如下所示

package com.example.phamf.javamvvmapp.Model;
import ...

@Entity (tableName = "location")
public class Location {

    @PrimaryKey(autoGenerate = true)
    private int id;

    private int type;        

    private float x;

    private float y;

    private String name;


    public Location(int id, float x, float y, String name, int type) {
        this.id = id;
        this.x = x;
        this.y = y;
        this.name = name;
        this.type = type;
    }

    public Location () {

    }

    // Getter and setter
}
这是我的数据库课

    @Database(entities = {Location.class}, version = 2)
    public abstract class LocationRoomDatabase extends RoomDatabase {

    private static LocationRoomDatabase DATABASE;

    public static LocationRoomDatabase getDatabase (final Context context) {

        Migration migration = new Migration(1, 2) {
            @Override
            public void migrate(@NonNull SupportSQLiteDatabase database) {
                database.execSQL("ALTER TABLE location ADD type INTEGER");
            }
        };

        if (DATABASE == null) {
            DATABASE = Room.databaseBuilder(context, LocationRoomDatabase.class, "database")
                           .addMigrations(migration).build();
    }

        return DATABASE;
    }

    public abstract LocationDAO locationDAO();
}
问题是当我运行上面的代码时,我得到了一个错误

//...

Caused by: java.lang.IllegalStateException:
   Migration didn't properly handle location(com.example.phamf.javamvvmapp.Model.Location).
Expected:
     TableInfo{..., type=Column{name='type', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=0}, foreignKeys=[], indices=[]}
Found:
     TableInfo{..., type=Column{name='type', type='INTEGER', affinity='3', notNull=false, primaryKeyPosition=0}}, foreignKeys=[], indices=[]}

//...
Caused by: android.database.sqlite.SQLiteException: Cannot add a NOT NULL column with default value NULL (code 1 SQLITE_ERROR): , while compiling: ALTER TABLE location ADD type INTEGER not null
期望的TableInfo和找到的TableInfo之间唯一不同的是“notNull值”。“期望”希望它是真实的,但“发现”却没有。我意识到问题就在那里,所以修改了迁移代码

        Migration migration = new Migration(1, 2) {
            @Override
            public void migrate(@NonNull SupportSQLiteDatabase database) {
                database.execSQL("ALTER TABLE location ADD type INTEGER not null");
            }
        };
我以为它会运行得很好,但后来我出错了

//...

Caused by: java.lang.IllegalStateException:
   Migration didn't properly handle location(com.example.phamf.javamvvmapp.Model.Location).
Expected:
     TableInfo{..., type=Column{name='type', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=0}, foreignKeys=[], indices=[]}
Found:
     TableInfo{..., type=Column{name='type', type='INTEGER', affinity='3', notNull=false, primaryKeyPosition=0}}, foreignKeys=[], indices=[]}

//...
Caused by: android.database.sqlite.SQLiteException: Cannot add a NOT NULL column with default value NULL (code 1 SQLITE_ERROR): , while compiling: ALTER TABLE location ADD type INTEGER not null
我也尝试过在“type”字段中添加一个默认值,但效果不太好

 private int type = 0;
任何解决这个问题的方法,请帮助我。非常感谢你。
为语法错误道歉。

我终于找到了解决办法。通过在SQL命令中的“…ADD COLUMN\u name”之后添加“DEFAULT someValue”。这使我的字段具有与预期表信息匹配的defaut值。 i、 e:


但这不是解决办法。如果将此应用于您的情况,则会出现重复列的异常