Ошибка по запросу, который работает с одним столбцом моей БД - PullRequest
1 голос
/ 04 ноября 2019

Я использую компоненты архитектуры Android с библиотекой Room. По сути, я хочу получить результаты из двух отдельных столбцов в двух разных запросах. Я буду рад, если кто-то может помочь или посоветовать мне, как систематизировать мою сущность, спасибо.

@Entity(tableName = "note_table")
public class Note {

    @PrimaryKey(autoGenerate = true)
    @ColumnInfo(name = "id_col")
    private int id;

    @Ignore
    @ColumnInfo(name = "checks_col")       
    private ArrayList<Checks> checks = new ArrayList<>();

    @ColumnInfo(name = "res_col")
    private String result;

    public Note(String result, ArrayList<Checks> checks) {
        this.checks = checks;
        this.result = result;
    }

    public void setId(int id) {this.id = id;}
    public int getId() {return id;}
    public void setResult(String result){this.result = result;}
    public String getResult() {return result;}
    public void setChecks(ArrayList<Checks> checks){this.checks = checks;}
    @TypeConverters({Converters.class})
    public ArrayList<Checks> getChecks() {return checks;}

}
@Dao
public interface NoteDao {

    @Insert
    void insert(Note note);

    @Update
    void update(Note note);

    @Delete
    void delete(Note note);

    @Query("DELETE FROM note_table")
    void deleteAllNotes();

    @Query("SELECT * FROM note_table")
    LiveData<List<Note>> getAllNotes();

    @Query("SELECT res_col FROM note_table ORDER BY res_col DESC LIMIT 1 ") // <-- this is my try //
    LiveData<Note> getResultNote();

}

Ошибка приведена ниже:

The columns returned by the query does not have the fields [id] . Even though they are annotated as non-null or primitive. Columns returned by the query: [res_col]

Предупреждающее сообщениеприведено ниже:

Note has some fields [id_col] 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: res_col. Fields in Note: id_col, res_col.

1 Ответ

0 голосов
/ 04 ноября 2019
LiveData<Note> getResultNote();

Хочет вернуть объект Note , так как некоторые столбцы аннотированы или подразумеваются как ненулевые (то есть private int id;, поскольку int является примитивом Java, тогдаоно не может быть нулевым) , тогда Примечание не может быть построено для String , которая должна быть возвращена запросом.

Вы хотите, чтобы getResultNote()вернуть Строка , а не Примечание .

Альтернативно используйте SELECT * ..... вместо SELECT res_col ...... для возврата Заметки, а затем получите строку из Заметки .

...