Android Room数据库查询不返回ID列。

13

问题是查询返回了除了'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)来抑制此警告。查询返回的列: name、start_date、end_date、description、icon_path、budget。app.aarsham.projeno.data.Model.Project中的字段: rowid、name、start_date、end_date、description、icon_path、budget。

还有一个错误:

查询返回的列中没有app.aarsham.projeno.data.Model.Project中的[id]字段,尽管它们被注释为非空或基元类型。查询返回的列:[name,start_date,end_date,description,icon_path,budget]

有人能帮忙解决吗?


从@Fts4项目类中删除 - user4571931
我曾经遇到过同样的问题,将实体中的 rowid 列删除后解决了错误。希望对你有所帮助。 - hkurokawa
我想知道实际使用FTS时该怎么做?因为我收到了相同的警告... - Kęstas Venslauskas
1个回答

31
当使用全文搜索(FTS)时,即使您使用select *选择所有行,也必须在查询中显式包含行“rowid”。基本上,查询应该像这样:@Query("SELECT *, `rowid` FROM projects")

1
我从来没有想过这种解决方案...谢谢! - FernandoH-G

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接