在带有 FTS4 的 Room 数据库中将 rowid 从 Long 更改为 Int

Change from Long to Int of `rowid` at Room Database w/ FTS4

我正在使用新的库版本更新我的应用程序,但我的数据库出现问题。发布的应用程序当前使用带有 FTS4 的 Room 版本 2.2.6。目前有 rowid 的 LONG。该应用程序运行流畅,没有问题。但我想使用 Room 版本 2.3.0 并根据文档:

An FTS entity table always has a column named rowid that is the equivalent of an INTEGER PRIMARY KEY index. Therefore, an FTS entity can only have a single field annotated with PrimaryKey, it must be named rowid and must be of INTEGER affinity. The field can be optionally omitted in the class but can still be used in queries.

我应该使用 INT 而不是 LONG。

@Entity(tableName = "visit")
@Fts4
public class Visit {

    @PrimaryKey
    @ColumnInfo(name = "rowid")
    private Long identification; //Change to int

}

有没有办法在不破坏用户数据的情况下更新 属性?

An FTS entity table always has a column named rowid that is the equivalent of an INTEGER PRIMARY KEY index.

简而言之,SQLite 的 INTEGER 并不意味着 Java/Kotlin Integer/Int/int 它是一个列 affinity/type.

如果您查看 Datatypes in SQLite3,那么一个 INTEGER 最多可以有 8 个字节(64 位有符号)。在 Java/Kotlin 中是 long/Long.

进一步的证据可以通过 SQLiteDatabase insert 便捷方法看到,因为它 returns 插入行的 id (rowid) 不是 int 而是 long。

Returns long - the row ID of the newly inserted row, or -1 if an error occurred.

SQLiteAutoincrement 解释了 rowid 理论上它可以是 1-9223372036854775807(你甚至可以有负值)。

因此,理论上 rowid 使用 int/Int/Integer 是错误的(实际上不太可能),rowid 可以大于 int/Int/Integer.

I should use INT not LONG.

我建议你应该使用 Long。它对数据没有影响,因为 SQLite 将尽可能少地存储整数 space 。此外,它对 room 创建的 table(s) 没有影响,因为 COLUMN TYPE 将为 INTEGER。