Updated 8 June 2018
Hey, in this blog we will see how to use room database with Kotlin programming language.
I have worked on room database for past 3-4 months. Now I have switched to Kotlin instead of java for few weeks. It is really cool.
Kotlin is a great fit for developing Android applications, bringing all of the advantages of a modern language to the Android platform without introducing any new restrictions:
The Room persistence library provides an abstraction layer over SQLite to allow fluent database access while harnessing the full power of SQLite.
Before, getting into the implementation part, let’s understand the basic components of Room.
There are basically 3 major components in Room.
For more details please read this.
Step 1: First of all, Create a Model class means a column for our database, add an Entity class
1234567891011121314151617181920212223242526272829303132333435363738394041424344 @Entity(tableName = "administrator")class Administrator {@PrimaryKey(autoGenerate = true)var uid: Int = 0@ColumnInfo(name = "email")private var email: String = ""@ColumnInfo(name = "password")private var password: String = ""@Ignoreprivate var newPassword: String = ""@Bindablefun getEmail(): String {if (email == null)return ""return email}fun setEmail(email: String) {this.email = email}@Bindablefun getPassword(): String {if (password == null)return ""return password}fun setPassword(password: String) {this.password = password}@Bindablefun getNewPassword(): String {if (newPassword == null)return ""return newPassword}fun setNewPassword(newPassword: String) {this.newPassword = newPassword}}
Step 2: Now, we need a DAO (Data Access Object), which will be used for accessing the database.
1234567891011121314 @Daopublic interface AdministratorDao {@Query("SELECT * FROM Administrator")Administrator getAll();@Query("SELECT * FROM Administrator WHERE uid IN (:AdministratorIds)")List<Administrator> loadAllByIds(int[] AdministratorIds);@Insertvoid insertAll(Administrator... Administrators);@Deletevoid delete(Administrator Administrator);}
Step 3: Finally, we need a DatabaseClass, annotated with Database. The Database class establishes a logical grouping between the DAO interfaces. It also defines the required version number, which is used to track and implement database migrations,
123456789101112131415161718 @Database(entities = arrayOf(Administrator::class), version = 1)abstract class AppDataBase : RoomDatabase() {companion object {private var mINSTANCE: AppDataBase? = nullpublic fun getAppDatabase(context: Context): AppDataBase {if (mINSTANCE == null) {mINSTANCE = Room.databaseBuilder(context.getApplicationContext(), AppDataBase::class.java, DB_NAME).build()}return mINSTANCE as AppDataBase}fun destroyDbInstance() {mINSTANCE = null}}}
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 laggy.
If you want to run your database query you have to add allowMainThreadQueries() in your database builder.
123 mINSTANCE = Room.databaseBuilder(context.getApplicationContext(), AppDataBase::class.java, DB_NAME).allowMainThreadQueries().build()
Now you have learned how to use Room database with kotlin. May be this blog is helpfull to you.
If you have any query please ask me on commet. Stay cool and stay updated. 🙂
If you have more details or questions, you can reply to the received confirmation email.
Back to Home
Be the first to comment.