> For the complete documentation index, see [llms.txt](https://android-course.cornellappdev.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://android-course.cornellappdev.com/chapters/7.-persistent-storage/data-access-objects.md).

# 5.5 Data Access Objects

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.

### Anatomy of a DAO <a href="#anatomy" id="anatomy"></a>

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 [`@Dao`](https://developer.android.com/reference/kotlin/androidx/room/Dao). 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:

```kotlin
@Dao
interface GroceryItemDao {
    // Get all GroceryItemEntitys
    @Query("SELECT * FROM groceryitementity")
    fun getAll(): List<GroceryItemEntity> 

    // Get all GroceryItemEntitys who's name is in the list `items`
    @Query("SELECT * FROM groceryitementity WHERE item IN (:items)")
    fun loadAllByIds(items: List<String>): List<GroceryItemEntity>

    // Insert the given GroceryItemEntity
    @Insert
    fun insertAll(vararg users: GroceryItemEntity)
    
    // Update the GroceryItemEntity given. Uses the primary key to match up the Entity
    @Update
    fun update(groceryItem: GroceryItemEntity)

    // Delete the given GroceryItemEntity by the primary key
    @Delete
    fun delete(groceryItem: GroceryItemEntity)
}
```

{% hint style="info" %}
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 [a quick rundown](https://backend-course.cornellappdev.com/chapters/databases/lecture#sql-operation-examples) of the SQLite language!
{% endhint %}

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.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://android-course.cornellappdev.com/chapters/7.-persistent-storage/data-access-objects.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
