Site icon Howtodoandroid

Easy Steps To Setup Room Database In Android

Room Database Android Example [Beginners] Poster

Room Database is a part of the Android Architecture components which provides an abstraction layer over SQLite which allows for more robust database access while still providing the full power of SQLite.

Room is a persistence library, part of the Android Jetpack

Before getting started, check out another post on the jetpack,

Getting started with WorkManager [Example]

Dependency injection on Android with Hilt[Example]

MVVM With Retrofit and Recyclerview in Kotlin [Example]

DataStore – Jetpack alternative for SharedPreference

Why use Room Database?

What is the difference between the room and the SQLite database?

Room architecture looks like the following:

There Are Basically 3 Major Components In Room.

1. @Entity

Entity Representation of table and columns become very easy. you have to annotate @Entity to a class and the name of the class becomes the table name and, data members become the name of the columns. The “@Entity” class represents an entity in a table.

@Entity(tableName = "user") data class Users(@PrimaryKey(autoGenerate = true)var userId: Int? = null,val userName: String, var location: String, val email: String)
Code language: JavaScript (javascript)

2. @Dao — Data Access Object

An Interface where we put all our SQL queries. We don’t require to write whole queries now; we need to make a method and annotate with specific annotations like

@Insert — Used to insert a record into the Room database.

@Delete — Used to delete records from the Room database.

@Update — Used to update records in Room Database.

@Query — Used to enter the Query like (SELECT FROM*)”

@Dao interface UserDao { @Insert fun insertUser(users: Users) @Query("Select * from user") fun gelAllUsers(): List<Users> @Update fun updateUser(users: Users) @Delete fun deleteUser(users: Users) }
Code language: CSS (css)

3. @Database

This is an abstract class that extends RoomDatabase, this is where you define the entities (tables)and the version number of your database. It contains the database holder and serves as the main access point for the underlying connection.

@Database(entities = [Users::class], version = 1, exportSchema = false) @TypeConverters(Converters::class) abstract class AppDatabase : RoomDatabase() { abstract fun userDao() : UserDao }
Code language: CSS (css)

Done with an explanation. Let’s start with the implementation part.


am going to create a sample app for user management using the room database in kotlin. In the app, I have added functionalities to insert, delete, update and list all the users.

Step 1 — Add dependencies

First, We need to add dependencies for the room database in our build.gradle file.

Step 2: Create a Model Class

The room creates a table for each class annotated with @Entity.

@Entity(tableName = "user") data class Users(@PrimaryKey(autoGenerate = true)var userId: Int? = null,val userName: String, var location: String, val email: String)
Code language: JavaScript (javascript)

Step 3 — Create DAO (Data Access Objects)

DAOs are responsible for defining the methods that access the database.

@Dao interface UserDao { @Insert fun insertUser(users: Users) @Query("Select * from user") fun gelAllUsers(): List<Users> @Update fun updateUser(users: Users) @Delete fun deleteUser(users: Users) }
Code language: CSS (css)

Here we just define basic SQL database functionality like inserting and deleting entries. You can see that the @Query annotation is used to annotate functions that are using queries.

You can also use parameters in your queries using: parametername .

Type Converters

Type Converters are used when we declare a property that Room and SQL don’t know how to serialize. Let’s see an example of how to serialize the List<String> data type.

class Converters { @TypeConverter fun fromString(value: String): List<String> { val listType = object : TypeToken<List<String>>() { }.type return Gson().fromJson(value, listType) } @TypeConverter fun fromArrayList(list: List<String>): String { val gson = Gson() return gson.toJson(list) } }
Code language: JavaScript (javascript)

Step 4 — Create the database

Now, Everything is ready to create a Database. We can create a Room Database by extending the RoomDatabase.

AppDatabase.kt

@Database(entities = [Users::class], version = 1, exportSchema = false) @TypeConverters(Converters::class) abstract class AppDatabase : RoomDatabase() { abstract fun userDao() : UserDao companion object { private var INSTANCE: AppDatabase? = null fun getInstance(context: Context): AppDatabase? { if (INSTANCE == null) { synchronized(AppDatabase::class) { INSTANCE = Room.databaseBuilder(context.applicationContext, AppDatabase::class.java, "user.db").allowMainThreadQueries() .build() } } return INSTANCE } fun destroyInstance() { INSTANCE = null } } }

Things to notice here:

Step 5 — CRUD operation on Room Database

Now the room database is ready for the CRUD operation.

UserRepository.kt

class UserRepository(context: Context) { var db: UserDao = AppDatabase.getInstance(context)?.userDao()!! //Fetch All the Users fun getAllUsers(): List<Users> { return db.gelAllUsers() } // Insert new user fun insertUser(users: Users) { insertAsyncTask(db).execute(users) } // update user fun updateUser(users: Users) { db.updateUser(users) } // Delete user fun deleteUser(users: Users) { db.deleteUser(users) } private class insertAsyncTask internal constructor(private val usersDao: UserDao) : AsyncTask<Users, Void, Void>() { override fun doInBackground(vararg params: Users): Void? { usersDao.insertUser(params[0]) return null } } }
Code language: HTML, XML (xml)

If you try running the above code with the created database above, your app will crash as the operation performed is on the main thread. By default, Room keeps a check of that and doesn’t allow operations on the main thread as it can make your UI Not respond.

You can avoid that by using AsyncTask or Handler or Rxjava with IO schedulers or any other options which put the operation on any other thread.

There is one more option, which allows you to do the operation on the main thread. You can use the same for testing but should avoid it. To do that you can add allowMainThreadQueries() on the builder.

Result of my room database android example.

Also, You can download the example on Github.

Android-Example/RoomAndroidExample at master · velmurugan-murugesan/Android-Example (github.com)

Conclusion

Thanks for reading.

Please try the room database in android with different queries to practice. And let me know your feedback in the comments.

Exit mobile version