在Sqlite中为具有LONG数据类型的字段的表创建房间实体 [英] Create Room Entity for a Table which has a field with LONG datatype in Sqlite

查看:527
本文介绍了在Sqlite中为具有LONG数据类型的字段的表创建房间实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

App数据库具有项目表,该表的列 Price 的数据类型为 Long . Db版本= 1

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)

在尝试迁移到Room时,我遇到以下问题

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}

这是我的Item实体类

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));
}
}

从SQLiteOpenHelper迁移到Room时,如何使Room实体字段支持Long数据类型.

How to make the Room entity field to support Long datatype when migration from SQLiteOpenHelper to Room.

推荐答案

简单的答案是您不能

房间仅支持5种数据类型,分别是TEXTINTEGERBLOBREALUNDEFINED.

Room only supports 5 data types which are TEXT, INTEGER, BLOB, REAL and UNDEFINED.

因此,BooleanIntegerLong的java数据类型将全部在 SQL 中转换为INTEGER.

So, java data types of Boolean, Integer, Long will be all converted to INTEGER in SQL.

您可以做的是在 SQL 中将LONG数据类型转换为INTEGER,而不是在 Room 中将INTEGER数据类型转换为LONG.以便让Room支持LONG,而Room不支持.

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.

这篇关于在Sqlite中为具有LONG数据类型的字段的表创建房间实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆