App Database has Items table with a column Price with datatype Long. Db Version = 1
CREATE TABLE items (_id INTEGER PRIMARY KEY AUTOINCREMENT,item_id
INTEGER,title TEXT,price LONG, UNIQUE (item_id) ON CONFLICT IGNORE)
While trying to migrate to Room I am experiencing below issue
java.lang.IllegalStateException: Migration didn't properly handle items(moka.pos.test.data.entity.Item).
Expected : price=Column{name='price', type='INTEGER', affinity='3', notNull=false, primaryKeyPosition=0}
Found : price=Column{name='price', type='LONG', affinity='1', notNull=false, primaryKeyPosition=0}
Here is my entity class for Item
@Entity(tableName = "items")
public class Item {
@PrimaryKey(autoGenerate = true)
@ColumnInfo(name = "_id")
private Integer _ID;
@ColumnInfo(name = "item_id")
private Integer id;
@ColumnInfo(name = "title")
private String title;
@ColumnInfo(name = "price")
private Long price;
public Integer get_ID() {
return _ID;
}
public void set_ID(Integer _ID) {
this._ID = _ID;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public Long getPrice() {
return price;
}
public void setPrice(Long price) {
this.price = (long) (getId() * AppUtil.getRandomNumber(10, 99));
}
}
How to make the Room entity field to support Long datatype when migration from SQLiteOpenHelper to Room.
The value is a signed integer, stored in 1, 2, 3, 4, 6, or 8 bytes depending on the magnitude of the value. Since long is 8 byte and INTEGER can also save values of 8 bytes, you can use INTEGER .
The Room persistence library provides an abstraction layer over SQLite to allow fluent database access while harnessing the full power of SQLite. In particular, Room provides the following benefits: Compile-time verification of SQL queries.
The simple answer is you CANNOT
Room only supports 5 data types which are TEXT
, INTEGER
, BLOB
, REAL
and UNDEFINED
.
So, java data types of Boolean
, Integer
, Long
will be all converted to INTEGER
in SQL.
What you can do is convert your LONG
data type to INTEGER
in SQL instead of converting INTEGER
data type to LONG
in Room in order to make Room support LONG
, which Room doesn't support.
As from the docs SQLITE doesn't support Long, check docs here.
INTEGER. The value is a signed integer, stored in 1, 2, 3, 4, 6, or 8 bytes depending on the magnitude of the value.
However as LONG is 8 byte and INTEGER can also save values of 8 bytes, you can use INTEGER.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With