Working with SQLite and Room Database

Tutorial 4 of 5

Working with SQLite and Room Database

1. Introduction

This tutorial aims to provide a comprehensive understanding of how to manage persistent data storage in Android using SQLite and Room Database. By the end of this tutorial, you will be proficient in creating a database, performing CRUD (Create, Read, Update, Delete) operations, and managing database versions and migrations.

What You Will Learn:

  • Basics of SQLite and Room Database
  • How to create a database
  • Performing CRUD operations
  • Managing database versions and migrations

Prerequisites:

  • Basic knowledge of Android development
  • Familiarity with Kotlin programming language

2. Step-by-Step Guide

SQLite and Room Database:

SQLite is a lightweight database that is used for mobile app development in Android. However, using SQLite directly can be a bit cumbersome. To simplify the work, Android introduced the Room persistence library, an abstraction layer over SQLite.

Creating a Database:

To create a database using Room, you need to define an interface that extends RoomDatabase. This interface is used as a database holder.

@Database(entities = [YourEntity::class], version = 1)
abstract class AppDatabase : RoomDatabase() {
    abstract fun yourDao(): YourDao
}

In this code, YourEntity is your data model class and YourDao is an interface containing methods for database operations.

Performing CRUD Operations:

CRUD operations are performed using DAO (Data Access Object). DAO is an interface that defines all the database operations that you want to perform.

@Dao
interface YourDao {
    @Query("SELECT * FROM yourEntity")
    fun getAll(): List<YourEntity>

    @Insert
    fun insertAll(vararg yourEntity: YourEntity)

    @Delete
    fun delete(yourEntity: YourEntity)
}

Managing Database Versions and Migrations:

Whenever you make a change in the database schema, you'll need to update the database version and provide a migration strategy.

val MIGRATION_1_2 = object : Migration(1, 2) {
    override fun migrate(database: SupportSQLiteDatabase) {
        database.execSQL("ALTER TABLE YourEntity ADD COLUMN new_column INTEGER")
    }
}

3. Code Examples

Example 1: Creating a Database

@Database(entities = [User::class], version = 1)
abstract class AppDatabase : RoomDatabase() {
    abstract fun userDao(): UserDao
}

Example 2: Inserting Data

val db = Room.databaseBuilder(
        applicationContext,
        AppDatabase::class.java, "database-name"
).build()

val userDao = db.userDao()
val user = User(name = "John", age = 30)
userDao.insertAll(user)

Example 3: Reading Data

val users = userDao.getAll()
for (user in users){
    println(user.name)
}

4. Summary

In this tutorial, you've learned the basics of SQLite and Room Database, how to create a database, perform CRUD operations, and manage database versions and migrations.

For further learning, you can explore more complex queries and database relations in Room.

5. Practice Exercises

  1. Create a Room Database for a blog app, where each article has a title, content, and author. Implement CRUD operations.

  2. Implement a migration from version 1 to version 2 of your blog app, where you add a 'publishedDate' column to your article table.

Remember, practice is key to mastering any skill. Happy coding!