ROOM Database - #1 Database Initialization | Introduction to ROOM Database | Android Studio Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey there everyone welcome to another video fox android so in this video we're gonna learn how to implement room db to our android application right as you guys must know that room db is a part of our android architecture components so earlier we used to uh use sqlite for writing the data to our local database through our android application right but we'll be using sqlite right now also but room db is a layer on top of c collect so with the help of room db library will be writing data to our local database and that is what is recommended by google these days to use the local database so we'll learn how to implement this to our android application how to make this schema of this and how to do various operations read write delete and update right so we'll be doing this in a couple of videos right two or three videos three videos at max right so in the first video we'll learn how to create a scheme what all code we need to write to implement it and what all libraries are required right we'll be doing it from scratch so let me just show you what we can get by the end of this video so if you can see on my emulator i have this app open and here i have two three fields first name last name roll number and tap on delete all all the data has been deleted so this is i'm just showing you various operations that can be done right and also we'll learn how to update the data i haven't made that screen yet as we'll be learning all these operations will implement update as well right so this is what you will mistake i have written view model i have just modified it and synced the project make sure you write it this way only so okay we are done with the dependencies and all the stuff that you can write as many queries that you want according to your requirement but i have written the basic queries so the very first query is if we want to get all the students from the database right the table then we'll get all the students in the form of a list so i have written a query select all from student table so it will give me all the students from the table and i'll get it in the form of list and the type of things that will contain the list will contain is of student type rate then we have function to uh find by roll number so if we want as in the illustration i've shown you guys we have received a student with the help of roll number so written a query select all from student table where roll number like roll number because we'll get this as an argument to this function so i have limited to one so if for example uh five students is having roll number three right so if we just put three it will give all the five students but i have limited to one so the very first student that will get it will give us that very student right so this next method is of insert so if you can see here on conflict i have just passed some argument inside this annotation so basically you have many methods on on conflict basically on conflict is if two students is having same role number right so what do you want it really depends on your application if you can have two duplicate uh entries then you can just pass this on cliff conflict strategy dot ignore so it will ignore and it will put to duplicate data so it really depends on your application so basically this function will take the student and it will put that uh we are done with our app database class as well so here you can see we have created an instance reference variable to the app database right so we have initialized it to null if it is not pointing to anything then we created a method called get database right so what this method will do is it will uh it will be called in some other class so if the instance the current instance is equal to null sorry it's not equal to null then we'll return the instance the existing instance to that particular class and it'll take the context as an argument but in case uh the first time app database is created and we do not have a current instance of that so what we'll do is we'll create a data instance of that so you can see we have used this uh database builder right we have passed the context the app database class the name of the database here you can uh give any name you want dot build and we have given we have passed this instance here and we have written the instance so it can be uh this instance as well it doesn't matter right so this is how when whenever a user calls dot get database method a new database will be created in case it is the first time that the table is not created yet right so this method needs to be called and in case it is already called so only single instance will be created at any given point of time in the whole application and same instance will be used so i i assume that you guys know what this synchronized means so basically this keyword means that uh if it is being called from three different places then all the all the operations will not be executed at a single point of time first it will wait for single operation to be completed then the next thread can carry out its operation and similarly this subsequent operations right so this is very important in case of dbs and all these operations so that's why we use synchronized and one more thing i forgot to tell you so you can see we have defined these functions as suspend functions because because we'll be calling these functions in core routines right because this is i operation it can take a longer period of time so you'll be calling it through core routines so that's why we have defined it as suspend function right so that is pretty much everything that we need to uh code to initialize the room database right all the things have been done now we just need to create the db right in the main activity we need to create the instance of that right lead and all the operations need to be done that will be doing in the next video so i assume that you have got the basic architecture so what what is required the very first thing is we need to implement the libraries here right second thing we are going to create a data class then this data class will define the object that we want to store inside our table then we have dow of that particular object that will have all the queries right read write operation all the queries will be written inside now then we are going to create a app database class that will create the instance to the database and that is pretty much everything all these three things needs to be done so for using room db so i guess uh you guys can listen to the code but still if you're in route make sure you ask that out in the comment section you can dm me on instagram the instagram username will be there on screen right now and i hope that you guys have liked this video in case you like the video make sure to hit that thumbs up button so this is it for today's video see you in another video bye [Music] you
Info
Channel: Foxandroid
Views: 39,003
Rating: undefined out of 5
Keywords: android room database tutorial, room database android with example, android room vs sqlite, room entity and data access object dao, learn android programming, what are android architecture components, android jetpack tutorial, room database, android room, kotlin, android room db kotlin, room entity, room database android, android architecture components, room db example, android studio tutorial, room, android studio, android room example, DAO, android room query, foxandroid
Id: NS7yYdW3Lho
Channel Id: undefined
Length: 18min 33sec (1113 seconds)
Published: Wed Jun 22 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.