# Database

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

*** ** * ** ***

Common/AllAndroid/JVMNative/C/iOSJavaScript
- Cmn

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

*** ** * ** ***

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

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

You can create an implementation of the class via androidx.room3.Room.databaseBuilder or androidx.room3.Room.inMemoryDatabaseBuilder.

```kotlin
// 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/kotlin/androidx/room3/Entity` or `https://developer.android.com/reference/kotlin/androidx/room3/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/kotlin/androidx/room3/Dao` classes. Using DAO classes will allow you to abstract the database communication in a more logical layer which will be much easier to stub in tests (compared to running direct SQL queries). It also automatically does the conversion from the results of a `https://developer.android.com/reference/kotlin/androidx/sqlite/SQLiteStatement` 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/kotlin/androidx/room3/Dao` classes while the application is being compiled so that if there is a problem in one of the queries, you will be notified during compilation.

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/kotlin/androidx/room3/AutoMigration` annotations. Note that if `https://developer.android.com/reference/kotlin/androidx/room3/Database#autoMigrations()` is not empty, `exportSchema` must be `true`.

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

## Summary

| ### Public constructors |
|---|---|
| `https://developer.android.com/reference/kotlin/androidx/room3/Database#Database(kotlin.Array,kotlin.Array,kotlin.Int,kotlin.Boolean,kotlin.Array)( entities: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-array/index.html<https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.reflect/-k-class/index.html<*>>, views: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-array/index.html<https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.reflect/-k-class/index.html<*>>, version: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html, exportSchema: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html, autoMigrations: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-array/index.html<https://developer.android.com/reference/kotlin/androidx/room3/AutoMigration> )` | Cmn |

| ### Public properties |
|---|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-array/index.html<https://developer.android.com/reference/kotlin/androidx/room3/AutoMigration>` | `https://developer.android.com/reference/kotlin/androidx/room3/Database#autoMigrations()` List of AutoMigrations that can be performed on this Database. | Cmn |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-array/index.html<https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.reflect/-k-class/index.html<*>>` | `https://developer.android.com/reference/kotlin/androidx/room3/Database#entities()` The list of entities included in the database. | Cmn |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html` | `https://developer.android.com/reference/kotlin/androidx/room3/Database#exportSchema()` You can set the annotation processor argument (`room.schemaLocation`) to tell Room to export the database schema into a folder. | Cmn |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | `https://developer.android.com/reference/kotlin/androidx/room3/Database#version()` The database version. | Cmn |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-array/index.html<https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.reflect/-k-class/index.html<*>>` | `https://developer.android.com/reference/kotlin/androidx/room3/Database#views()` The list of database views included in the database. | Cmn |

## Public constructors

### Database

Cmn

```
Database(
    entities: Array<KClass<*>> = [],
    views: Array<KClass<*>> = [],
    version: Int,
    exportSchema: Boolean = true,
    autoMigrations: Array<AutoMigration> = []
)
```

## Public properties

### autoMigrations

Cmn

```
val autoMigrations: Array<AutoMigration>
```

List of AutoMigrations that can be performed on this Database.

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

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

| Returns |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-array/index.html<https://developer.android.com/reference/kotlin/androidx/room3/AutoMigration>` | List of `https://developer.android.com/reference/kotlin/androidx/room3/AutoMigration` annotations. |

### entities

Cmn

```
val entities: Array<KClass<*>>
```

The list of entities included in the database. Each class represents a table in the database.

| Returns |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-array/index.html<https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.reflect/-k-class/index.html<*>>` | The list of entities in the database. |

### exportSchema

Cmn

```
val exportSchema: Boolean
```

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. If you do export schemas then 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 value 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 |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html` | Whether the schema should be exported to the given folder when the `room.schemaLocation` argument is set. Defaults to `true`. |

### version

Cmn

```
val version: Int
```

The database version.

| Returns |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | The database version. |

### views

Cmn

```
val views: Array<KClass<*>>
```

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

| Returns |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-array/index.html<https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.reflect/-k-class/index.html<*>>` | The list of database views. |