【问题标题】:Query in Room Database in Android Studio在 Android Studio 中的房间数据库中查询
【发布时间】:2018-07-25 22:46:52
【问题描述】:

我在房间数据库中有一个笔记列表。我想根据某些特征选择特定注释列表。对于这个提议,我将第二个字段 note_second_id 添加到 PrimaryKey:

 @PrimaryKey(autoGenerate = true)
        private long note_id;  
        private long note_second_id;

然后我创建下一个查询:

@Query("SELECT * FROM " + Constants.TABLE_NAME_NOTE + " WHERE  note_second_id = :second_id ")
    List<Note> getNotes(long second_id);

当我想添加一个新的笔记时,我使用:

          long j = activityReference.get().dataBase.getNoteDao().insertNote(note);
          deck.setNote_id(j);
          deck.setNote_second_id(note000);

其中方法 .setNote_id() 和 setNote_second_id() 如下所示:

 public void setNote_id(long deck_id) {this.note_id = note_id; }    
 public void setNote_second_id(long notesecondid) {    
 this.note_second_id = notesecondid; }

并且 note000 是一个常数:

Long note000 = 11111L;

在活动中,我希望显示这些列表:

 private class RetrieveTaskDeck extends AsyncTask<Void,Void,List<Deck>> {
         private WeakReference<MainActivity> activityReference;
         // only retain a weak reference to the activity
       RetrieveTaskDeck(MainActivity context) {
           activityReference = new WeakReference<>(context);        }

       @Override
        protected List<Note> doInBackground(Void... voids) {
           if (activityReference.get()!=null)                
                return  activityReference.get().dataBase.getNotesDao().getNotes(note000); 
            else
                return null;     }

       @Override
        protected void onPostExecute(List<Note> notes) {
            if (notes !=null && notes.size()>0 ){
                activityReference.get().notes.clear();
                activityReference.get().notes.addAll(notes);
                // hides empty text view 
           activityReference.get().textViewMsgDeck.setVisibility(View.GONE);
            activityReference.get().decksAdapter.notifyDataSetChanged();   }   }  }

问题是这种方式行不通。它不显示任何注释。只有当我使用查询时:

   @Query("SELECT * FROM " + Constants.TABLE_NAME_NOTE)
        List<Note> getAllNotes();

activityReference.get().dataBase.getNotesDao().getAllNotes();

只有这样我才能得到我拥有的所有笔记的列表。但我只需要显示一些特别标记的注释。

出了什么问题,我该怎么办?查询似乎有问题...也许有使用房间数据库和查询经验的人可以提供一些建议...

【问题讨论】:

    标签: android database sqlite android-studio android-room


    【解决方案1】:

    其实我已经解决了这个问题。这是一个解决方案。 我使用了下一个查询:

    @Query("SELECT * FROM " + Constants.TABLE_NAME_NOTE + " where note_content LIKE :second_id ")
    List<Note> getNotes(String second_id);
    

    其中“note_content”是列名,“second_id”是第一行,写在列名之后:

    @ColumnInfo(name = "note_content")
    private String second_id; 
    

    注意,如果我在列声明后的第一个字符串值(第二个或第三个)之后的某处写String second_id,它不会通过这个词进行搜索。但是通过这种方式,如上所述,它可以工作。

    所以,我可以根据任务动态设置该列的字符串值“second_id”,用户甚至不需要知道它是否存在。我尝试在 PrimaryKey 中使用 ID 来解决相同的任务,但没有成功。实际上,在 Column 中使用 String 值是有效的。希望它会帮助某人。此外,如果有人知道如何使用 PrimaryKey 中的 ID 解决相同的任务,请告诉我在这里编写您的解决方案。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-09-27
      • 1970-01-01
      • 1970-01-01
      • 2020-05-06
      • 2021-05-21
      • 2020-05-01
      • 2020-04-05
      • 2020-06-13
      相关资源
      最近更新 更多