Create Room Entity for a Table which has a field with LONG datatype in Sqlite

点点圈 提交于 2020-06-24 14:15:53

问题


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.


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/54035138/create-room-entity-for-a-table-which-has-a-field-with-long-datatype-in-sqlite

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!