0Pricing
Jetpack Compose Academy · レッスン

エンティティ、DAO、データベース

Roomでローカルスキーマを定義します。

「エンティティ、DAO、データベース」はCoddyKit上の無料Jetpack Compose Academyレッスンです。 これはレッスン1/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはJetpack Compose Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Jetpack Compose Academyコースには全4レッスンが含まれています。

このレッスンの一部はまだ翻訳されておらず、英語で表示されています。

Room Is Your Local SQLite

Room is a friendly layer over SQLite. It lets you save data on the device with plain Kotlin classes instead of raw SQL strings. 💾

Three Core Pieces

Room has exactly three building blocks: an Entity for the table, a DAO for the queries, and a Database that ties them together.

An Entity Is a Table

Annotate a data class with @Entity and Room turns it into a database table, one column per property.

@Entity
data class Note(val text: String)

Every Row Needs a Key

Mark one field as the @PrimaryKey so each row is uniquely identified. Use autoGenerate to let Room assign ids for you.

@PrimaryKey(autoGenerate = true)
val id: Int = 0

The DAO Holds Your Queries

A @Dao interface declares the operations you want. You write the intent, and Room generates the implementation.

@Dao
interface NoteDao {
    // queries go here
}

Insert With One Annotation

Add a function and tag it with @Insert. Room writes the SQL that saves your entity to the table.

@Insert
suspend fun insert(note: Note)

Read With @Query

Use @Query with real SQL to fetch rows. Room checks the query against your schema at compile time.

@Query("SELECT * FROM Note")
suspend fun getAll(): List<Note>

Update and Delete Too

The same pattern covers writes: @Update and @Delete change existing rows by matching their primary key.

@Delete
suspend fun delete(note: Note)

The Database Class

Declare an abstract class with @Database, list your entities, and expose each DAO. This is the single entry point.

@Database(entities = [Note::class], version = 1)
abstract class AppDb : RoomDatabase() {
    abstract fun noteDao(): NoteDao
}

Build the Database Once

Create the instance with Room.databaseBuilder. Building it is expensive, so do it a single time and reuse it.

Room.databaseBuilder(context, AppDb::class.java, "app.db").build()

Suspend Keeps the UI Smooth

Disk access is slow, so mark DAO functions suspend. Room then runs them off the main thread and never freezes your screen.

Quick Check

Which annotation marks a Kotlin class as a Room database table?

Recap: The Three Pillars

You defined an Entity table, a DAO of suspend queries, and a Database that wires them up. Next you will make those reads reactive.

よくある質問

「エンティティ、DAO、データベース」レッスンは無料ですか?

はい。「エンティティ、DAO、データベース」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Jetpack Compose Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Jetpack Compose Academyコースには全4レッスンが含まれています。

「エンティティ、DAO、データベース」で何を学びますか?

Roomでローカルスキーマを定義します。 ブラウザで直接実行するハンズオンコードでJetpack Compose Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

Jetpack Compose Academyを始めるのに経験は必要ですか?

事前経験は必要ありません。CoddyKitのJetpack Compose Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン1/4です。

「エンティティ、DAO、データベース」レッスンにはどのくらい時間がかかりますか?

ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。

このJetpack Compose Academyレッスンでコードを書いて実行できますか?

はい。すべてのJetpack Compose Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。

このコースのすべてのレッスン

  1. エンティティ、DAO、データベース
  2. Flowを返すリアクティブクエリ
  3. Repositoryパターン
  4. オフラインファーストのキャッシュ戦略
← Jetpack Compose Academyに戻る