gpt4 book ai didi

android - 与我的数据库的单个列一起使用的查询出错

转载 作者:行者123 更新时间:2023-11-30 04:58:35 24 4
gpt4 key购买 nike

我正在使用带有 Room 库的 Android 架构组件。基本上我想从两个不同查询中的两个单独的列中获取结果。如果有人可以帮助我或建议我如何系统化我的实体,我会很高兴,谢谢。

@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.

最佳答案

LiveData<Note> getResultNote();

想要返回一个 Note 对象,因为某些列被注释或暗示为非空 (即 private int id; as int 是 Java 原语,那么它不能为 null) 则不能为查询返回的 String 构建一个 Note。 p>

您希望 getResultNote() 返回一个 String 而不是 Note

或者使用 SELECT * ..... 而不是 SELECT res_col ...... 来返回 Note,然后从 中获取字符串注意

关于android - 与我的数据库的单个列一起使用的查询出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58691797/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com