• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Why won't my Android Room Update my Table?

Guitarist

Newbie
This is an issue I had posted elsewhere (StackOverflow and JavaRanch) but I had to abandon 5 months ago. Now that I'm back on this project this issue is still present and I'm not sure how to resolve it. I never received a comment or suggestion on previous posts and thought someone may have a help or suggestion here.

My problem: I cannot get the Android Room @update method calls on my DAOs to actually update my tables. Below is my code and you can reference my previous posts if you like (StackOverflow and JavaRanch). Below, I will be using the Question table and value as the example.

The Notes table acts as a Join table. Each note contains the unique primary keys of up to 7 tables with related data - including the Questions table.
Initialization of the Notes:

Java:
Notes updatedNote;
updatedNote = rdb.getNotesDao().getNote(vNoteID);

How I'm trying to update:
The 'valuesAreDifferent' boolean method evaluates the previous question value against the current question value. If they are different then a Questions object is created. The question's unique ID is retrieved from the updatedNote for the current Question and passed. Then the Question object is passed to the database Questions DAO to be updated with the @update method created.

NOTE: the 3rd line was an additional attempt to get the update to work but I'm not exactly sure it is needed.

Code:
if(!valuesAreDifferent(original.get(4), update.get(4))){
      Questions questions = rdb.getQuestionsDao().getQuestion(updatedNote.getQuestionID());
      questions.setQuestion(update.get(4));
      rdb.getQuestionsDao().updateQuestion(questions);
}

The Questions Entity and DAO:

Code:
@Entity(tableName = "Questions")
public class Questions {
    @PrimaryKey(autoGenerate = true)
    @ColumnInfo(name = "QuestionID")
    private int questionID;
    @ColumnInfo(name = "Question")
    private String question;
    public Questions(String question){
        this.question = question;
    }
/*    @Ignore
    public Questions(int questionID, String question){
        this.questionID = questionID;
        this.question = question;
    }*/
    public int getQuestionID() {
        return questionID;
    }
    public void setQuestionID(int questionID) {
        this.questionID = questionID;
    }
    public String getQuestion() {
        return question;
    }
    public void setQuestion(String question) {
        this.question = question;
    }
}

@Dao
public interface QuestionsDao {
    @Insert
    void addQuestion(Questions question);
    @Update
    void updateQuestion(Questions question);
    @Delete
    void deleteQuestion(Questions question);
    @Query("SELECT * FROM Questions ORDER BY Question")
    List<Questions> getQuestions();
    @Query("SELECT * FROM Questions WHERE QuestionID = :questionID")
    Questions getQuestion(int questionID);
    @Query("SELECT last_insert_rowid()")
    int lastQuestionPKID();
    @RawQuery
    List<Integer> customSearchQuestionsTable(SupportSQLiteQue
 
Last edited:
Back
Top Bottom