# 5.6 Databases

### Creating the Database

The database does the heavy lifting of storing and retrieving your entities through the abstract functions of your DAO.&#x20;

The following code defines an `GroceryDatabase`class to hold the database. `GroceryDatabase`defines the database configuration and serves as the app's main access point to the persisted data. The database class must satisfy the following conditions:

* The class must be annotated with a [`@Database`](https://developer.android.com/reference/kotlin/androidx/room/Database) annotation that includes an [`entities`](https://developer.android.com/reference/kotlin/androidx/room/Database#entities) array that lists all of the data entities associated with the database.
* The class must be an abstract class that extends [`RoomDatabase`](https://developer.android.com/reference/kotlin/androidx/room/RoomDatabase).
* For each DAO class that is associated with the database, the database class must define an abstract method that has zero arguments and returns an instance of the DAO class.

```kotlin
@Database(entities = [GroceryItem::class], version = 1)
abstract class GroceryDatabase: RoomDatabase() {
    abstract fun groceryItemDao(): GroceryItemDao
}
```

### Accessing the Database

After you have defined the data entity, the DAO, and the database object, you can use the following code to create an instance of the database:

```kotlin
val db = Room.databaseBuilder(
            applicationContext,
            GroceryDatabase::class.java, "grocery-database"
        ).build()
```

{% hint style="danger" %}
If you are using your database across your app, you should only need one instance of the Room Database class. Having the number of instances of a Database can lead to memory leaks. Therefore one should use the [singleton ](/chapters/7.-persistent-storage/6.1-singletons.md)patterns in the Database to keep one unified instance!
{% endhint %}

From there, you can call any of the methods defined in your DAO:

```kotlin
val groceryItemDao = db.groceryItemDao()
groceryItemDao.insert(groceryItem)
val groceryItems = groceryItemDao.getAll()
...
```

Note: since database access can take a long time, we want to run it in the background--otherwise the UI thread can be blocked, causing the app to freeze or just be generally slow. The Rooms library can be combined with [Coroutines ](/chapters/5.5-concurrency/7.2-coroutines.md)to help mitigate this issue!


---

# Agent Instructions: 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:

```
GET https://android-course.cornellappdev.com/chapters/7.-persistent-storage/databases.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
