Share
Explore

Understanding @Insert and Other Room Annotations

Let's understand what these @ symbols (annotations) mean in our database code. Think of annotations like sticky notes that give special instructions to Room! 🏷️
Learning Outcomes:
The intersection between
Interfaces
CRUD operations ROOM APIs

What are Annotations?

```kotlin // This is an annotation @Insert suspend fun addStudent(student: StudentEntity)
// This is also an annotation, but with some extra information @Query("SELECT * FROM students") fun getAllStudents(): List<StudentEntity>
Think of it like this:- Annotations are messages to Room (our database helper) - They start with @ (like a label) - They tell Room what kind of database operation to perform

The Basic CRUD Annotations 🏷️

```kotlin interface StudentDao { // CREATE: "Hey Room, please insert this into the database" @Insert suspend fun addStudent(student: StudentEntity)
// READ: "Hey Room, please find this in the database" @Query("SELECT * FROM students") fun getAllStudents(): List<StudentEntity>
// UPDATE: "Hey Room, please change this in the database" @Update suspend fun updateStudent(student: StudentEntity)
// DELETE: "Hey Room, please remove this from the database" @Delete suspend fun deleteStudent(student: StudentEntity) } ```

How Room Uses These Annotations 🔨

When Room sees an annotation, it generates the actual database code for us:
```kotlin // When we write this: @Insert suspend fun addStudent(student: StudentEntity)
// Room creates something like this behind the scenes: suspend fun addStudent(student: StudentEntity) { database.execSQL( "INSERT INTO students (id, name, gpa) VALUES (?, ?, ?)", arrayOf(student.id, student.name, student.gpa) ) } ```

Types of Annotations We Use 📝

1. **@Insert** - For adding new data ```kotlin @Insert suspend fun addStudent(student: StudentEntity) // Adds one student
@Insert suspend fun addManyStudents(students: List<StudentEntity>) // Adds many students ```
2. **@Query** - For custom database questions ```kotlin @Query("SELECT * FROM students WHERE gpa >= :minGpa") fun getHighGpaStudents(minGpa: Double): List<StudentEntity> ```
3. **@Update** - For changing existing data ```kotlin @Update suspend fun updateStudent(student: StudentEntity) ```
4. **@Delete** - For removing data ```kotlin @Delete suspend fun deleteStudent(student: StudentEntity) ```

Simple Example Using All Types 🌟

```kotlin @Dao interface StudentDao { // INSERT example @Insert suspend fun addStudent(student: StudentEntity) // Room will create the INSERT SQL for us!
// QUERY example @Query("SELECT * FROM students") fun getAllStudents(): List<StudentEntity> // Room will create the SELECT SQL for us!
// UPDATE example @Update suspend fun updateStudent(student: StudentEntity) // Room will create the UPDATE SQL for us!
// DELETE example @Delete suspend fun deleteStudent(student: StudentEntity) // Room will create the DELETE SQL for us! } ```

Remember 🎯

1. Annotations start with @ 2. They're like instructions to Room 3. Room reads these instructions and creates the actual database code 4. We don't need to write SQL ourselves!
Think of annotations like post-it notes: - @Insert = "Please put this in the database" - @Query = "Please find this in the database" - @Update = "Please change this in the database" - @Delete = "Please remove this from the database"

Practice Exercise 📝

Extend the provided code base by writing these annotations:
1. Write an @Insert to add multiple students at once
2. Write a @Query to find students by name
3. Write an @Update to update multiple students
4. Write a @Delete to delete all students with low GPAs

Want to print your doc?
This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (
CtrlP
) instead.