房间数据库中的布尔查询

时间:2020-03-18 07:08:41

标签: android android-studio android-sqlite android-room android-jetpack

我要从网络上获取对象,然后将其直接插入 ROOM DATABASE

@Entity(tableName = Constants.TABLE_OBJECT_ICONS)
public class ObjectClass {

    @PrimaryKey()
    @SerializedName("id")
    @ColumnInfo(name = Constants.KEY_OBJECT_ID)
    @Expose
    private Integer id;

    @SerializedName("controlUnit")
    @ColumnInfo(name = Constants.KEY_CONTROL_UNIT)
    @Expose
    private String controlUnit;

    @SerializedName("isStack")
    @ColumnInfo(name = Constants.IS_STACK)
    @Expose
    private Boolean isStack;

    public Boolean getStack() {
        return isStack;
    }

    public void setStack(Boolean stack) {
        isStack = stack;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getControlUnit() {
        return controlUnit;
    }

    public void setControlUnit(String controlUnit) {
        this.controlUnit = controlUnit;
    }

}

现在的问题是,我正在将 Boollen 数据类型插入数据库,并且房间正在将其映射到0和1,但是当我尝试获取数据时,我的 getStack()方法返回null

更新

这是 Dao

@Dao
public interface ObjectDao {

    @Insert
    void insertObject(ObjectClass objectClass);

    @Query("DELETE FROM object_icons")
    void deleteAllObjects();

    @Query("SELECT * FROM object_icons WHERE object_id=:id AND site_id=:siteId")
    Single<ObjectClass> getObjectById(String id, String siteId);

    @Query("SELECT object_name FROM object_icons WHERE object_id=:id AND site_id=:siteId")
    String getObjectNameById(String id, String siteId);

    @Query("SELECT * FROM object_icons WHERE object_keyword=:key AND site_id=:siteId")
    ObjectClass getObjectByKey(String key, String siteId);

    @Query("SELECT * FROM object_icons WHERE site_id=:siteId")
    Single<List<ObjectClass>> getObjectIconsList(String siteId);

}

,在获取数据库后,我将其打开到 SQLite Online 中,下面是数据库的结构。

enter image description here

1 个答案:

答案 0 :(得分:0)

在阅读对象时,参考此answer房间应将1转换为true。同样,如果您要从Sqlite迁移到Room,则您可能会遇到BooleanInteger类型的问题,我不知道这是否是您的情况,但以下是一个链接,以防万一: room migration using existing boolean column types