Updating the Database

The database serves as the single source of truth for crime data. In CriminalIntent, when the user leaves the detail screen, any edits they made should be saved to the database. (Other apps might have other requirements, like having a “save” button or saving updates as the user types.)

First, add a function to your CrimeDao to update an existing crime. While you are at it, also add a function to insert a new crime. You will mostly ignore the insert function for now, but you will use it in Chapter 14 when you add a menu option to the UI to create new crimes.

Listing 12.13  Adding update and insert database functions (database/CrimeDao.kt)

@Dao interface CrimeDao { @Query("SELECT * FROM crime") fun getCrimes(): ...

Get Android Programming: The Big Nerd Ranch Guide, 4th Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.