为 Table 创建房间实体,它在 Sqlite 中有一个 LONG 数据类型的字段
Create Room Entity for a Table which has a field with LONG datatype in Sqlite
App 数据库有 Items table 列 Price 数据类型 Long.
数据库版本 = 1
CREATE TABLE items (_id INTEGER PRIMARY KEY AUTOINCREMENT,item_id
INTEGER,title TEXT,price LONG, UNIQUE (item_id) ON CONFLICT IGNORE)
尝试迁移到 Room 时遇到以下问题
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}
这是我的实体 class 的项目
@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 数据类型。
简单的答案是你不能
Room只支持TEXT
、INTEGER
、BLOB
、REAL
、UNDEFINED
、
5种数据类型
因此,Boolean
、Integer
、Long
的java数据类型在[=中将全部转换为INTEGER
41=].
您可以将 LONG
数据类型转换为 SQL 中的 INTEGER
而不是转换 INTEGER
数据在 Room 中键入 LONG
以使 Room 支持 LONG
,而 Room 不支持。
由于文档 SQLITE 不支持 Long,请在此处查看 docs。
INTEGER. The value is a signed integer, stored in 1, 2, 3, 4, 6, or 8
bytes depending on the magnitude of the value.
然而LONG是8个字节,INTEGER也可以保存8个字节的值,你可以使用INTEGER .
如果有人还在为此苦苦挣扎,就像我几个小时所做的那样,并且不理解上述解决方案...只需在 SQLite 中创建一个新的数据库文件,复制 SQL 语句用于创建 table 但将每个数据类型更改为 TEXT、INTEGER、BLOB、REAL 和 UNDEFINED。然后用从旧数据库复制的 SQL 语句填充 table。修复了简单地更改 SQLite 中的数据类型没有的问题。
App 数据库有 Items table 列 Price 数据类型 Long. 数据库版本 = 1
CREATE TABLE items (_id INTEGER PRIMARY KEY AUTOINCREMENT,item_id
INTEGER,title TEXT,price LONG, UNIQUE (item_id) ON CONFLICT IGNORE)
尝试迁移到 Room 时遇到以下问题
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}
这是我的实体 class 的项目
@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 数据类型。
简单的答案是你不能
Room只支持TEXT
、INTEGER
、BLOB
、REAL
、UNDEFINED
、
因此,Boolean
、Integer
、Long
的java数据类型在[=中将全部转换为INTEGER
41=].
您可以将 LONG
数据类型转换为 SQL 中的 INTEGER
而不是转换 INTEGER
数据在 Room 中键入 LONG
以使 Room 支持 LONG
,而 Room 不支持。
由于文档 SQLITE 不支持 Long,请在此处查看 docs。
INTEGER. The value is a signed integer, stored in 1, 2, 3, 4, 6, or 8 bytes depending on the magnitude of the value.
然而LONG是8个字节,INTEGER也可以保存8个字节的值,你可以使用INTEGER .
如果有人还在为此苦苦挣扎,就像我几个小时所做的那样,并且不理解上述解决方案...只需在 SQLite 中创建一个新的数据库文件,复制 SQL 语句用于创建 table 但将每个数据类型更改为 TEXT、INTEGER、BLOB、REAL 和 UNDEFINED。然后用从旧数据库复制的 SQL 语句填充 table。修复了简单地更改 SQLite 中的数据类型没有的问题。