Tăn*_*Tài 2 android android-room
我想使用 room 在我的数据库中添加一个新列。我使用 Room 的迁移将我的数据库迁移到新版本,但它无法正常工作。INTERGER 字段的默认 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
}
Run Code Online (Sandbox Code Playgroud)
我想添加一个名为 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
}
Run Code Online (Sandbox Code Playgroud)
这是我的 RoomDatabase 类。
@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();
}
Run Code Online (Sandbox Code Playgroud)
问题是当我运行上面的代码时,出现错误
//...
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=[]}
//...
Run Code Online (Sandbox Code Playgroud)
Expected TableInfo 和Found TableInfo 之间唯一不同的是“notNull 值”。Expected 希望它是真的,但 Found 不是。我意识到问题就在那里,所以我修改了我的迁移代码
Migration migration = new Migration(1, 2) {
@Override
public void migrate(@NonNull SupportSQLiteDatabase database) {
database.execSQL("ALTER TABLE location ADD type INTEGER not null");
}
};
Run Code Online (Sandbox Code Playgroud)
我以为它会完美运行,但后来出现错误
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
Run Code Online (Sandbox Code Playgroud)
我还尝试向“类型”字段添加默认值,但它也不起作用
private int type = 0;
Run Code Online (Sandbox Code Playgroud)
有什么办法可以解决这个问题,请帮助我。非常感谢。为任何语法错误道歉。
我终于找到了解决方案。通过在 SQL 命令中的“... ADD COLUMN column_name”之后添加“DEFAULT someValue”。这使我的字段具有与预期表信息匹配的默认值。IE :
sql.execSQL("ALTER TABLE table_name ADD COLUMN column_name INTEGER DEFAULT 1 not null")
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2325 次 |
| 最近记录: |