Android App Development
iOS App Development
Flutter App Development
Cross Platform App Development
Hire on-demand project developers and turn your idea into working reality.
Big thanks to Webkul and his team for helping get Opencart 3.0.3.7 release ready!
Deniel Kerr
Founder. Opencart
Top Partners
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.
Android Room Persistence Library
Step 1: First of all, Create a Model class means a column for our database, add an Entity class
@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 = "" @Ignore private var newPassword: String = "" @Bindable fun getEmail(): String { if (email == null) return "" return email } fun setEmail(email: String) { this.email = email } @Bindable fun getPassword(): String { if (password == null) return "" return password } fun setPassword(password: String) { this.password = password } @Bindable fun getNewPassword(): String { if (newPassword == null) return "" return newPassword } fun setNewPassword(newPassword: String) { this.newPassword = newPassword } } 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 = "" @Ignore private var newPassword: String = "" @Bindable fun getEmail(): String { if (email == null) return "" return email } fun setEmail(email: String) { this.email = email } @Bindable fun getPassword(): String { if (password == null) return "" return password } fun setPassword(password: String) { this.password = password } @Bindable fun 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.
@Dao public interface AdministratorDao { @Query("SELECT * FROM Administrator") Administrator getAll(); @Query("SELECT * FROM Administrator WHERE uid IN (:AdministratorIds)") List<Administrator> loadAllByIds(int[] AdministratorIds); @Insert void insertAll(Administrator... Administrators); @Delete void delete(Administrator Administrator); } 1234567891011121314 @Daopublic interface AdministratorDao { @Query("SELECT * FROM Administrator") Administrator getAll(); @Query("SELECT * FROM Administrator WHERE uid IN (:AdministratorIds)") List<Administrator> loadAllByIds(int[] AdministratorIds); @Insert void insertAll(Administrator... Administrators); @Delete void 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,
@Database(entities = arrayOf(Administrator::class), version = 1) abstract class AppDataBase : RoomDatabase() { companion object { private var mINSTANCE: AppDataBase? = null public 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 } } } 123456789101112131415161718 @Database(entities = arrayOf(Administrator::class), version = 1)abstract class AppDataBase : RoomDatabase() { companion object { private var mINSTANCE: AppDataBase? = null public 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.
mINSTANCE = Room.databaseBuilder(context.getApplicationContext(), AppDataBase::class.java, DB_NAME) .allowMainThreadQueries() .build() 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. 🙂
Your email address will not be published. Required fields are marked*
Name*
Email*
Save my name email and website in this browser for the next time I comment.
Be the first to comment.
We use cookies to personalize your experience. By continuing to visit this website you agree to our use of cookies. Learn more about privacy policy
We've bought a Shopware Extension which is a hybrid App, which we can now offer in the Google Play Store and the IOS App Store. I'am still impressed how fast and how sharp the support team leads us through every process of the App-Store clarification. Very fast and useful response.
Markus Walter
Founder, Das Apartment Living
USA
India
Global
Name
Email
Enquiry or Requirement
If you have more details or questions, you can reply to the received confirmation email.