【问题标题】:Android Room Persistance Library - SQlite: debugging gradle errorAndroid Room Persistance Library - SQlite:调试 gradle 错误
【发布时间】:2019-07-11 23:01:32
【问题描述】:

刚刚设置了一个Sqlite db,我第一次成功插入,正在尝试在代码中进行更新查询,突然android无法编译。收到此错误:

Gradle may disable incremental compilation as the following annotation processors are not incremental: room-compiler-2.1.0.jar (androidx.room:room-   compiler:2.1.0), lifecycle-compiler-2.0.0.jar (androidx.lifecycle:lifecycle-compiler:2.0.0).
Consider setting the experimental feature flag android.enableSeparateAnnotationProcessing=true in the gradle.properties file to run annotation processing in a separate task and make compilation incremental.
C:\app\src\main\java\com\example\persistence\repositories\NoteRepository.java:74: error: method update in interface NoteDao cannot be applied to given types;
        noteDao.update(notes[0]);
               ^
 required: int,int
 found: Note

Repository 中与此方法相关的错误:

private static class UpdateNoteAsyncTask extends AsyncTask<Note, Void, Void>{

private NoteDao noteDao;

private UpdateNoteAsyncTask(NoteDao noteDao){
this.noteDao = noteDao;
}

@Override
   protected Void doInBackground(Note... notes) {

   noteDao.update(notes[0]);
        return null;
   }
 }

数据访问类:

@Dao
public interface NoteDao {

@Insert
void insert(Note note);

@Query("UPDATE note_table SET distortions= :distortions WHERE cbtId= :cbtId")
void update(int cbtId, int distortions);

@Delete
void delete(Note note);

【问题讨论】:

    标签: android sqlite android-sqlite android-room


    【解决方案1】:

    您将update 定义为:

    @Query("UPDATE note_table SET distortions= :distortions WHERE cbtId= :cbtId")
    void update(int cbtId, int distortions);
    

    所以你应该像这样使用它:

    update(int cbtId, int distortions)
    

    当你像这样使用它时:

    noteDao.update(notes[0]);
    

    Gradle 需要两个参数。这里说它找到了 Note 对象(你的论点中的那个)。

        required: int,int
        found: Note
    

    【讨论】:

    • noteDao.update(notes[0]);此行位于 ViewModel 类中,因此那里不存在这 2 个 int 变量。它是如何工作的?
    • 如果我将代码更改为:@Override protected Void doInBackground(Note... notes) { noteDao.update(int cbtId, int twists);返回空值;它不会接受它,整数只有红线,IDE 说它期待 );,
    • 你可以尝试调用 noteDao.update(1,2); 吗?只是看看它是否会更新您的数据库。
    • 如果没有 pk id,它怎么知道要更新哪一行?
    • 您使用“WHERE condition”“找到”更新的行。您的条件是“ cbtId= :cbtId ” 当您执行此语句时,您告诉数据库查找 condition 为真的相应行。在这种情况下,您的条件是 cbtId 等于参数(对于您的语句,它是一个名为 cbtId 的变量)。然后当执行“SET twists= :distortions”时,列扭曲下的行部分被更新。这些都是 SQL 语句,我建议花一些时间研究关系数据库:w3schools.com/sql 是一个很好的资源。
    猜你喜欢
    • 2018-06-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-05
    • 2020-05-10
    相关资源
    最近更新 更多