# DatabaseView

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

*** ** * ** ***


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

<br />

*** ** * ** ***

Marks a class as an SQLite view.

The value of the annotation is a SELECT query used when the view is created.

The class will behave like normal POJO when it is used in a `https://developer.android.com/reference/androidx/room/Dao`. You can SELECT FROM a `https://developer.android.com/reference/androidx/room/DatabaseView` similar to an `https://developer.android.com/reference/androidx/room/Entity`, but you can not INSERT, DELETE or UPDATE into a `https://developer.android.com/reference/androidx/room/DatabaseView`.

Similar to an `https://developer.android.com/reference/androidx/room/Entity`, you can use `https://developer.android.com/reference/androidx/room/ColumnInfo` and `https://developer.android.com/reference/androidx/room/Embedded` inside to customize the data class.

Example:

```
@DatabaseView(
  "SELECT id, name, release_year FROM Song " +
  "WHERE release_year >= 1990 AND release_year <= 1999"
)
data class SongFrom90s (
  val id: Long,
  val name: String,
  @ColumnInfo(name = "release_year")
  val releaseYear: Int
)
```

Views have to be registered to a RoomDatabase via `https://developer.android.com/reference/androidx/room/Database#views()`.

| See also |
|---|---|
| `https://developer.android.com/reference/androidx/room/Dao` |   |
| `https://developer.android.com/reference/androidx/room/Database` |   |
| `https://developer.android.com/reference/androidx/room/ColumnInfo` |   |
| `https://developer.android.com/reference/androidx/room/Embedded` |   |

## Summary

| ### Public methods |
|---|---|
| `final @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/lang/String.html` | `https://developer.android.com/reference/androidx/room/DatabaseView#getValue()()` The SELECT query. |
| `final @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/lang/String.html` | `https://developer.android.com/reference/androidx/room/DatabaseView#getViewName()()` The view name in the SQLite database. |

## Public methods

### getValue

```
public final @NonNull String getValue()
```

The SELECT query.

| Returns |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/lang/String.html` | The SELECT query. |

### getViewName

```
public final @NonNull String getViewName()
```

The view name in the SQLite database. If not set, it defaults to the class name.

| Returns |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/lang/String.html` | The SQLite view name. |