Anything you must know about ROOM DATABASE in Android Jetpack Compose.

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello my friends and welcome back to another great video in this video I'm going to show you how to work with room database this is a crash course about room database and I try to keep it as simple as possible uh I will show you all of the requirements to build uh room database and use it insert data into it update data into it and delete data from the uh tables of the room database uh using mvvm and uh as I said it's a crash course and it's a short approach to uh room datab and in the next videos we are going to build some practical applications using room database to make you more familiar with room database uh be with me till the end of this video to get knowledge about the the main structure of a database let's get started here I have an empty project and I have added these uh lines of dependencies this and we are using cha cut name annotation uh processing something like that and to make it valid you should add this plugin as well after syncing we are ready to use the uh materials that we need uh as I said we are going to work uh on room database and uh the first thing that we need to uh use a database to create a database is entry point of the data uh this is a data class and here I'm going to create a package calling it room DB and inside it I'm going to uh separate uh the classes of the room database from other classes new class data class let's call it note we are going to build the simple note application so we need here some variables one of them note name the type a string and not body again the type a screen uh sorry a string and another value for the uh notes ID the type integer equals to Z by semic color okay uh as I said we are going to use the annotations for each part to uh tell the room database hey this is your entry point this is the point that uh the user can have access to the data or something like that uh The annotation of this data class is entity and as I said that means that is the entry point of the data uh each uh part of each row of our database contains this name this body and this ID uh the ID must be unique to make it unique we are going to use another annotation primary key and inside of this autogenerate should be true uh so anytime that we are adding a new uh row to our database this integer uh will be created by the database automatically and we don't need to add any ID uh to our uh variable of the database item of the database let's say okay that's it for this uh data class and let's go to the next level here uh we need an interface and let's call it uh room d a o uh the interface room Dao and Dao is a data access object if I don't make mistakes so d a o and we are telling our uh database that hey this is your doll okay inside of this uh interface we should create all of the functions that we need so we should think uh how we want to react uh with our database what you want to do with our database and for each of these activities uh we have to create one uh function like adding data and in the database language we call it insert data then update data delete data and retrieve all of the data fetch all of the data to display them so four functions and uh nowadays inside of this room database you have another annotation call it upsert it's mixed of update and insert so we can merge these two functions to insert and the update and use upsert instead of these two functions so we need need three functions upsert delete and retrieve all of the data fch all of the data let's create them we need the suspen function for upsert notes and we have to pass a note to it and it has its own annotation calling it upsert so that's it and let's press contr D to duplicate it uh instead of this delete and time delete add uh query and we need the query to retrieve the data and here we need a normal function not a suspend function and let's call it get all uh notes and no need to pass anything to it and we should write a simple query select star from uh note that's it so we have now our functions and uh we can create the uh database class itself now so let's create it new class and calling it note datase okay here we have the note database and uh we should make it abstract here uh let's tell it hey that you are a room database and The annotation should be the database let's see what the do we have here here we need entity so we should uh connect it to this and we need views we need version export schema Auto migrations and I think only the version and the entity is obligatory so let's add this two only and entities will be uh not uh class and version one more col version number one and that's it here we need on abstract value calling it Dao and R that's it uh for this class okay we have our database ready to use now uh we need only uh somewhere to call these functions to set a data to our database or retrieve the data but we cannot simply call them inside our main activity and it's not uh uh like based on the standard so let's see uh what we can do here we are going to create a repository and uh view model so we should separate the UI and the data layer from each other if you uh make the instance of database here in the main activity and try to set the data you can do it but when you want to retrieve the data the compiler gives you error that uh you cannot run this on the main thread and to separate it from the uh UI thread it's uh uh obligatory to use something like view model so let's do it I'm going to create another package here calling it view model let's have a repository inside it a new class repository closing it in the body of it we need a database uh let's create a value private value DB typ of it um it should be room database now let's create the functions uh like the functions that we had here in the uh doll uh let me copy this and paste it there and edit it we don't need the annotations here so let's get rid of them and here we need to have body for so let's use the DB dot uh sorry it should be note database let's press alt and enter and here yeah we have it and optimize the UT okay you have it uh do upsert note and pass uh the note here okay that's it we need a body for this function uh to delete them DB uh Dot do do delete note passing note to e and finally here it can be equal to DB do do do get all notes and there is uh no need to pass anything here okay the repository also is done now and we can create our view model let's create if you don't know what is view model and how to use it you can find the video about the view model in my channel link will be in the description okay we have a class calling it not view model uh we need to pass in variable let's have it like private private value Ripple itory AO repository and uh let's tell it hey you a VI model and that's it now we need to have some functions here as well so a function uh for get notes and equals [Music] to Ripple do get all notes dot as uh live data Let's uh go to the not here the Ripple not here yeah in the doll uh this get all notes uh should uh give us a list of our notes I forgot about it and I'm going to use uh flow for this and and then I will convert this flow to live data to have the live data so simple uh let's go to the view model and now we can use this import live data and uh here we just need to write VI model scope. cod in context okay we have whatever we want here for uh retrieving all of the data and now we need another function calling it upsert note passing a note to it view modelos scope do launch you're are launching view model scope because the functions that we want to call here are suspen functions so repository do upset not and passing note to it let's press control D to duplicate it and instead of this use delete notes and instead of this we it not okay we have whatever we need to uh launch our project but uh before that we have to go to main activity to uh create the actual database and set some data on it so in the main activity first we should create in the global scope a private while calling it DB by lazy and as I used it too much in the previous videos by lazy is something like uh lat it variable room do database Builder we need to pass some uh items here so first of all application context then the class of our database so it was note database class. Java and you need a name not n uh it should be something like no DB and do build that's it we have created our database and now let's introduce our um vew model view model by models we need parentheses here and uh here we are going to pass the class of not view mod okay some of you asked in the last videos uh what is the factory and uh when we are going to use it or something like this is the answer of your question when whenever uh here we are passing something like repository in the Constructor of the view model class then we should use the factory and this is the most simple answer that I can give you to this question so here we need a uh Factory producer Lambda block inside of it uh we need object uh view model provider do Factory again uh Lambda black and inside it overwrite function this one [Music] return Noe uh VI model and here we have to pass a repository so repository and for the repository we have to pass the DB so we pass the DB instance that we create here to the repository and finally as T just use it like that okay uh everything is is ready let's add some data to our database to add data we need some variables uh let me create them a v we need for name uh by remember mutable state of MTS string Alt Enter Alt Enter okay control D to duplicate it another variable I need for the body of the note and I need another variable for the note itself equals to note and here I'm going to pass the name and the body okay now we need to pass this data to our database how let's have a column uh adding of maybe 12. TP and vertical Arrangement Arrangement Space by 12 DP that's good on the top let's have a button a text inside of the button to tell set data and for the click uh for the on click let's call The View model do upsert note and pass the note into it that's enough to send the data to the database but here down below let's have some text Fields uh uh to take the data from the user name and here uh name equals to e and a placeholder equals to a text and the text will be a hint so again a name let's press contr D duplicate it uh body and again body and here body that's it I think to set the data let's check it on emulator okay here we have this three dots and as you can see if you don't have it here actually this is the app uh inspection if you don't have it here you can find it inside of this when you are clicking on it you just need to select the device that you have and it show you the database situation the database status of this application as you can see there is no database here as soon as I'm sending the first item to my database it should be created automatically here now let's take a look at it after sending the data hello uh world and let's set data okay you can see it we have the note DB by this name that uh we gave it here and we have the first row of our database by the ID of one uh body of word name of hello and hello here hello Android it's uh in my datab base but I have to sync it and you can see the next item is here and we can have another item mkr develop let's check it again okay here we have the third item and you can see automatically we have a new ID for this uh let's retrieve all of this data and display them here in a lazy column down below where is this uh column and it's here okay a lazy lazy lazy column okay uh for the lazy column let's use items uh block and this one with the list itself not with the count of the list so here and here and now we need the list and before we set it on the lazy column we have to uh set the data on it so let's create a v calling it note list uh by remember uh mutable state of list St and here let's add the notes that's it now let's set the data on it the view model dot get noes do Observer this and Lambda BL and here note list equals to it and that's it now let's pass the note list into it and it will give us a note for each index and let's have a column here um inside of this column let's have two text one text for the name equals to uh dollar sign [Music] note dot not name and control D Body equals to note body and here let's have a spacer height of uh DP and at the end let's have a divider is field Max V and do padding of 6. DP we have whatever we need uh to show the items in the laser column let's check it okay here we have the data that we set on uh the data Bas and let's add another data and see if uh really we have the live data live flow and we can see uh the new data here immediately so the name Android and here something like that body and set data and you can see we have it here immediately like live let's set more and more and you can see the data are available here and now let's see how we can delete the data let's make this [Music] column uh clickable and here view model do delete note and we have to pass the note into it that's it let's Che it again okay in the inspection we have the data here and here I'm going to click on one of them and you can see I can delete them let's sync it and id1 and ID3 uh are here available but the rest of data are missing now and we could delete them successfully and one more that's it okay that was everything I wanted to tell you about room database in this video uh that was like a simple approach to room database and you can use it but uh uh if you want to use it in actual project be patient in the next videos I'm going to build some practical project with your database and we will expand it and we will use more safe ways to use the room database so that's it uh if you like this video please give me a thumbs up uh if you are new in the Channel please subscribe to the channel and if you want to financially support me you can find the link of the buy me a coffee down below in the description click on it and buy me a coffee and I will be so happy and also you can uh click on the thanks and buy me a super thanks as well have a good day byebye
Info
Channel: MkrDeveloper
Views: 1,314
Rating: undefined out of 5
Keywords: Mkr Developer, jetpack compose ROOM DATABASE, ROOM DATABASE in jetpack compose. Android studio | Kotlin, ROOM DATABASE - jetpack compose, android jetpack compose ROOM DATABASE, Mohsen mashkour, android jetpack compose, jetpack compose, Jetpack compose in android kotlin, jetpack compose mohsen mashkour, clean architecture, jetpack compose tutorials for beginners, android studio, pagination in compose, ROOM DATABASE, android room database, android room db, room database, library
Id: DNLOC6syy1Q
Channel Id: undefined
Length: 34min 31sec (2071 seconds)
Published: Tue Apr 16 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.