#1- Introduction to ROOM Database
Articles Blog

#1- Introduction to ROOM Database

February 22, 2020

Hey guys, Its Vignesh from CodeAndroid In this video will see about ROOM Database ROOM is a Jetpack library, which sits on top of SQLiteDatabase and provides all the feature of SQLite at the same time allows to access the data without much of the code Why we need to use ROOM Database Compile-time Verification In SQLiteDatabase, if the query is wrong then it will cause run-time exception Because Android Studio won’t do any validation while building the app But when we use ROOM Database the queries are validated at compile-time So that It will give the error message while typing the query itself Boilerplate code When we use native SQLite we need to write lot of code to convert cursor object to DTO object In ROOM Database we we don’t need to write any code for this convertion Internally ROOM Database itself will handles that Integration with other components As it is an Jetback Library Integration with other componets like LiveData or RxJava is simple Now will see the basic components of ROOM Database There are 3 components Database, Dao and Entity First will see about the Entity To create a table in ROOM Database We need to create a class and annotate the class with @Entity Whatever fields are there in that class will be considered as the column for the table To create the Primary key we need to annotate one of the field as @PrimaryKey In case we want to create primary key as autogenerated number then we need to set the annotation like this @PrimaryKey inside we need to pass autoGenerate=true Database To create a database in ROOM, we need to create an class and annotate it with @Database Inside we need to give two information what are all the tables should be there in this database and database version Here User and Credentials are the tables inside the db and 1 is the database version Dao This is the place where we do the database opration To create the Dao we need to create an interface and annotate it with @Dao. Inside this interface we need to create the method declaration required for db opertion For doing the database operation we need to annotate the function using one of the following annotation Query, Insert, Update and Delete To run an RAW query we need to annotate the function with @Query inside we need to pass the Database Query To insert the data into database we need take the entiry object as the parameter Also we need to annotate the function with @Insert So that ROOM Database handles the insertion For update also we need to take the entity object as a parameter and annotate the function with @update Finally to delete the record from the database we need to pass the entity object and annotate the function with @Delete In this case object which is matching the Primary key will get deleted In case you want to delete using where class then we need to use the @Query annotation That’s all for this tutorials guys If you like this video please give a thumbs-up If you have any doubt please put it in the comment section I will try to answer it Hope you like it I will see you in my next video Have a nice day

Leave a Reply

Your email address will not be published. Required fields are marked *