1. Are you ready for the Galaxy S20? Here is everything we know so far!

Why won't my Android Room Update my Table?

Discussion in 'Android Development' started by Guitarist, Sep 23, 2020.

  1. Guitarist

    Guitarist Newbie
    Thread Starter

    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:

    Code (Java):
    1. Notes updatedNote;
    2. 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 (Text):
    1. if(!valuesAreDifferent(original.get(4), update.get(4))){
    2.       Questions questions = rdb.getQuestionsDao().getQuestion(updatedNote.getQuestionID());
    3.       questions.setQuestion(update.get(4));
    4.       rdb.getQuestionsDao().updateQuestion(questions);
    5. }
    The Questions Entity and DAO:

    Code (Text):
    1. @Entity(tableName = "Questions")
    2. public class Questions {
    3.     @PrimaryKey(autoGenerate = true)
    4.     @ColumnInfo(name = "QuestionID")
    5.     private int questionID;
    6.     @ColumnInfo(name = "Question")
    7.     private String question;
    8.     public Questions(String question){
    9.         this.question = question;
    10.     }
    11. /*    @Ignore
    12.     public Questions(int questionID, String question){
    13.         this.questionID = questionID;
    14.         this.question = question;
    15.     }*/
    16.     public int getQuestionID() {
    17.         return questionID;
    18.     }
    19.     public void setQuestionID(int questionID) {
    20.         this.questionID = questionID;
    21.     }
    22.     public String getQuestion() {
    23.         return question;
    24.     }
    25.     public void setQuestion(String question) {
    26.         this.question = question;
    27.     }
    28. }
    29.  
    30. @Dao
    31. public interface QuestionsDao {
    32.     @Insert
    33.     void addQuestion(Questions question);
    34.     @Update
    35.     void updateQuestion(Questions question);
    36.     @Delete
    37.     void deleteQuestion(Questions question);
    38.     @Query("SELECT * FROM Questions ORDER BY Question")
    39.     List<Questions> getQuestions();
    40.     @Query("SELECT * FROM Questions WHERE QuestionID = :questionID")
    41.     Questions getQuestion(int questionID);
    42.     @Query("SELECT last_insert_rowid()")
    43.     int lastQuestionPKID();
    44.     @RawQuery
    45.     List<Integer> customSearchQuestionsTable(SupportSQLiteQue
     


    #1 Guitarist, Sep 23, 2020
    Last edited: Sep 23, 2020
Loading...

Share This Page

Loading...