waseefakhtar
waseefakhtar

Reputation: 1423

How to update existing rows in Room database?

I have a DAO class for updating a note if a note already exists for a particular date. If note, the DAO creates a new note. Here's the DAO interface:

@Insert(onConflict = OnConflictStrategy.REPLACE)
public long insertNote(Note note);

@Update
public int updateNote(Note note);

Here's how I read all the data from the Database:

viewModel = ViewModelProviders.of(this).get(NoteListViewModel.class);
viewModel.getNoteList().observe(MainActivity.this, new Observer<List<Note>>() {
      @Override
      public void onChanged(@Nullable List<Note> noteList) {
            Log.d("Note List Items", noteList.toString());
      }
});

Now, I don't know to check, compare and update existing rows in the database.

Upvotes: 0

Views: 4179

Answers (1)

CommonsWare
CommonsWare

Reputation: 1006584

Your Note @Entity class has a primary key. updateNote() will update the row based on that primary key.

Upvotes: 3

Related Questions