首页 > 解决方案 > Android Room 数据库查询不返回 id 列

问题描述

问题是查询返回除“id”之外的所有列

我使用 fts4,在文档中它说:

启用 FTS 的表始终使用 INTEGER 类型的主键和列名“rowid”。如果您的 FTS 表支持的实体定义了主键,则它必须使用该类型和列名。

这是我的实体类:

@Fts4
@Entity(tableName = "projects")
public class Project {

    @ColumnInfo(name = "rowid")
    @PrimaryKey(autoGenerate = true)
    private int id;
    private String name;
    @ColumnInfo(name = "start_date")
    private String startDate;
    @ColumnInfo(name = "end_date")
    private String endDate;
    private String description;
    @ColumnInfo(name = "icon_path")
    private String iconPath;
    private long budget;


public Project(String name, String startDate, String endDate, String description, String iconPath, long budget) {
    this.name = name;
    this.startDate = startDate;
    this.endDate = endDate;
    this.description = description;
    this.iconPath = iconPath;
    this.budget = budget;
}


public int getId() {
    return id;
}

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

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getStartDate() {
    return startDate;
}

public void setStartDate(String startDate) {
    this.startDate = startDate;
}

public String getEndDate() {
    return endDate;
}

public void setEndDate(String endDate) {
    this.endDate = endDate;
}

public String getDescription() {
    return description;
}

public void setDescription(String description) {
    this.description = description;
}

public String getIconPath() {
    return iconPath;
}

public void setIconPath(String iconPath) {
    this.iconPath = iconPath;
}

public long getBudget() {
    return budget;
}

public void setBudget(long budget) {
    this.budget = budget;
}

这是我的简单查询:

@Query("SELECT * FROM projects")
public LiveData<List<Project>> getAllProjectsI);

我收到警告:

app.aarsham.projeno.data.Model.Project 有一些字段 [rowid] 不由查询返回。如果不应该从结果中读取它们,则可以使用 @Ignore 注释对其进行标记。您可以通过使用 @SuppressWarnings(RoomWarnings.CURSOR_MISMATCH) 注释方法来抑制此警告。查询返回的列:名称、开始日期、结束日期、描述、图标路径、预算。app.aarsham.projeno.data.Model.Project 中的字段:rowid、name、start_date、end_date、description、icon_path、budget。

和一个错误:

查询返回的列在 app.aarsham.projeno.data.Model.Project 中没有字段 [id],即使它们被注释为非 null 或原始的。查询返回的列:[name,start_date,end_date,description,icon_path,budget]

有人可以帮忙吗?

标签: androidsqliteandroid-roomfts4

解决方案


使用 FTS 时,rowid即使您select *用于选择所有行,也必须在查询中显式包含“”行。

基本上,查询应该像@Query("SELECT *, `rowid ` FROM projects")


推荐阅读