5.5 Data Access Objects
Last updated
Was this helpful?
Last updated
Was this helpful?
When you use the Room persistence library to store your app's data, you interact with the stored data by defining data access objects, or DAOs. Each DAO includes methods that offer abstract access to your app's database.
You can define each DAO as either an interface or an abstract class. For basic use cases, you should usually use an interface. In either case, you must always annotate your DAOs with . DAOs don't have properties, but they do define one or more methods for interacting with the data in your app's database.
Here is an example DAO for our grocery item entity:
Note: Since primary keys must be unique to update an item that already exists in the database, you must use an update method, not an insert method. It will throw an exception if an insert is called with a primary key that already exists in the database.
You can additionally do more complex queries, deletions, updates, and insertions using more advanced SQL. For many cases, however, this will do. Feel free to check out the backend course for of the SQLite language!