Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 330
Karthikeyan Avatar asked Jan 04 '19 08:01

Karthikeyan


People also ask

What is long in SQLite?

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 .

What is room in SQLite?

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.


Video Answer


2 Answers

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.

like image 156
musooff Avatar answered Oct 16 '22 16:10

musooff


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.

like image 8
karan Avatar answered Oct 16 '22 18:10

karan