# Database

Artifact: [androidx.room:room-common](https://developer.android.com/jetpack/androidx/releases/room) [View Source](https://cs.android.com/search?q=file:androidx/room/Database.kt+class:androidx.room.Database) Added in [2.0.0](https://developer.android.com/jetpack/androidx/releases/room#2.0.0)

*** ** * ** ***


```
@Target(allowedTargets = [AnnotationTarget.CLASS])
@Retention(value = AnnotationRetention.BINARY)
public annotation Database
```

<br />

*** ** * ** ***

Marks a class as a `https://developer.android.com/reference/androidx/room/RoomDatabase`.

The class should be an abstract class and extend `https://developer.android.com/reference/androidx/room/RoomDatabase`.

You can receive an implementation of the class via androidx.room.Room.databaseBuilder or androidx.room.Room.inMemoryDatabaseBuilder.

```
// Song and Album are classes annotated with @Entity.
@Database(version = 1, entities = [Song::class, Album::class])
abstract class MusicDatabase : RoomDatabase {
  // SongDao is a class annotated with @Dao.
  abstract fun getSongDao(): SongDao

  // AlbumDao is a class annotated with @Dao.
  abstract fun getArtistDao(): ArtistDao

  // SongAlbumDao is a class annotated with @Dao.
  abstract fun getSongAlbumDao(): SongAlbumDao
}
```

The example above defines a class that has 2 tables and 3 DAO classes that are used to access it. There is no limit on the number of `https://developer.android.com/reference/androidx/room/Entity` or `https://developer.android.com/reference/androidx/room/Dao` classes but they must be unique within the Database.

Instead of running queries on the database directly, you are highly recommended to create `https://developer.android.com/reference/androidx/room/Dao` classes. Using Dao classes will allow you to abstract the database communication in a more logical layer which will be much easier to mock in tests (compared to running direct SQL queries). It also automatically does the conversion from `Cursor` to your application data classes so you don't need to deal with lower level database APIs for most of your data access.

Room also verifies all of your queries in `https://developer.android.com/reference/androidx/room/Dao` classes while the application is being compiled so that if there is a problem in one of the queries, you will be notified instantly.

To automatically generate a migration between two versions of the database, assuming you have the relevant schema files, you are recommended to use `https://developer.android.com/reference/androidx/room/AutoMigration` annotations. Note that if an autoMigration is defined in a database, `exportSchema` must be `true`.

| See also |
|---|---|
| `https://developer.android.com/reference/androidx/room/Dao` |   |
| `https://developer.android.com/reference/androidx/room/Entity` |   |
| `https://developer.android.com/reference/androidx/room/AutoMigration` |   |
| `https://developer.android.com/reference/androidx/room/RoomDatabase` |   |
| `https://developer.android.com/reference/androidx/room/ConstructedBy` |   |

## Summary

| ### Public methods |
|---|---|
| `final @https://developer.android.com/reference/androidx/annotation/NonNull AutoMigration[]` | `https://developer.android.com/reference/androidx/room/Database#getAutoMigrations()()` List of AutoMigrations that can be performed on this Database. |
| `final @https://developer.android.com/reference/androidx/annotation/NonNull KClass[]` | `https://developer.android.com/reference/androidx/room/Database#getEntities()()` The list of entities included in the database. |
| `final boolean` | `https://developer.android.com/reference/androidx/room/Database#getExportSchema()()` You can set the annotation processor argument (`room.schemaLocation`) to tell Room to export the database schema into a folder. |
| `final int` | `https://developer.android.com/reference/androidx/room/Database#getVersion()()` The database version. |
| `final @https://developer.android.com/reference/androidx/annotation/NonNull KClass[]` | `https://developer.android.com/reference/androidx/room/Database#getViews()()` The list of database views included in the database. |

## Public methods

### getAutoMigrations

```
public final @NonNull AutoMigration[] getAutoMigrations()
```

List of AutoMigrations that can be performed on this Database.

See `https://developer.android.com/reference/androidx/room/AutoMigration` for example code usage.

For more complicated cases not covered by `https://developer.android.com/reference/androidx/room/AutoMigration`, runtime defined `https://developer.android.com/reference/androidx/room/migration/Migration` added with `https://developer.android.com/reference/androidx/room/RoomDatabase.Builder#addMigrations(kotlin.Array)` can still be used.

| Returns |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull AutoMigration[]` | List of `https://developer.android.com/reference/androidx/room/AutoMigration` annotations. |

### getEntities

```
public final @NonNull KClass[] getEntities()
```

The list of entities included in the database. Each entity turns into a table in the database.

| Returns |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull KClass[]` | The list of entities in the database. |

### getExportSchema

```
public final boolean getExportSchema()
```

You can set the annotation processor argument (`room.schemaLocation`) to tell Room to export the database schema into a folder. Even though it is not mandatory, it is a good practice to have version history of your schema in your codebase and you should commit the schema files into your version control system (but don't ship them with your app!).

When `room.schemaLocation` is set, Room will check this variable and if it is set to `true`, the database schema will be exported into the given folder.

Value of `exportSchema` is `true` by default but you can disable it for databases when you don't want to keep history of versions (like an in-memory only database).

| Returns |
|---|---|
| `boolean` | Whether the schema should be exported to the given folder when the `room.schemaLocation` argument is set. Defaults to `true`. |

### getVersion

```
public final int getVersion()
```

The database version.

| Returns |
|---|---|
| `int` | The database version. |

### getViews

```
public final @NonNull KClass[] getViews()
```

The list of database views included in the database. Each class turns into a view in the database.

| Returns |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull KClass[]` | The list of database views. |