Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Room database query does not return id column

The problem is that the query returns all columns except 'id'

I use fts4 and in docs it says:

FTS-enabled tables always use a primary key of type INTEGER and with the column name "rowid". If your FTS-table-backed entity defines a primary key, it must use that type and column name.

here is my entity class:

@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;
}

and here is my simple query:

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

I got a warning :

app.aarsham.projeno.data.Model.Project has some fields [rowid] which are not returned by the query. If they are not supposed to be read from the result, you can mark them with @Ignore annotation. You can suppress this warning by annotating the method with @SuppressWarnings(RoomWarnings.CURSOR_MISMATCH). Columns returned by the query: name, start_date, end_date, description, icon_path, budget. Fields in app.aarsham.projeno.data.Model.Project: rowid, name, start_date, end_date, description, icon_path, budget.

and an error:

The columns returned by the query does not have the fields [id] in app.aarsham.projeno.data.Model.Project even though they are annotated as non-null or primitive. Columns returned by the query: [name,start_date,end_date,description,icon_path,budget]

can anyone help about this?

like image 989
Arsham Avatar asked Nov 29 '18 09:11

Arsham


People also ask

Has some fields Rowid which are not returned by the query?

Project has some fields [rowid] which are not returned by the query. If they are not supposed to be read from the result, you can mark them with @Ignore annotation. You can suppress this warning by annotating the method with @SuppressWarnings(RoomWarnings. CURSOR_MISMATCH).

What is the use of room database in Android?

Room is a persistence library that provides an abstraction layer over the SQLite database to allow a more robust database. With the help of room, we can easily create the database and perform CRUD operations very easily.

Is Android room an ORM?

Is Android Room an ORM? Room isn't an ORM; instead, it is a whole library that allows us to create and manipulate SQLite databases more easily. By using annotations, we can define our databases, tables, and operations.

What is room in SQLite?

Room is a persistent library that is part of the Android jetpack. It is built on top of SQLite.


1 Answers

When using FTS, you have to explicitly include the row "rowid" in your query even if you are using select * to select all rows.

Basically, the query should be like @Query("SELECT *, `rowid ` FROM projects")

like image 190
Jack Avatar answered Sep 18 '22 01:09

Jack