Android SQLite Database Tutorial 📱 Complete 1-HOUR SQLite Android Tutorial | Kotlin & Android Studio

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what's up youtube this is dennis panuta for tutorials.u in this video i've got a golden nugget for you because we are going to cover sqlite in depth this is going to be a one hour course on sqlite so how to use databases in android and using android studio to get even more information about the data that you have in your database so you're going to see a bunch of good tricks that are going to help you to become better at understanding databases and also at using them or at least at using sql lite which is a very lightweight database kind of structure that is a database directly on the phone itself on which the application will be installed in the end alright so i hope you enjoyed this video and please make sure to hit that like button and also leave a comment and while you're at it hit that subscribe button as well because i promise you you'll get a lot of value from this video it's by the way part of my complete android master class you can find a link in the description below where you will learn how to use what you're learning in this video to build full applications all right so let's dive into the video alrighty welcome back in this video we are going to check out sqlite or better said in this video we are going to check out the preparation for the sql lite demo which will be in the next video because i have this little application here which i prepared and you can see what it does so let's have a look at it first and then let's see how it's built from a structural perspective some from a ui perspective and then we are going to look at the database specifics because what we are using here is an item adapter which is again a recycle view adapter so we have seen recycler used before and we're also using pop-ups here or dialogues custom dialogues which are going be to be created so let's look at this we have this little application i call the sqlite demo but what it really does is it stores employee data so we have the name and the email id of that employee so i'm just going to enter something here so my name is dennis panyuta and my email address will be testheadtest.com and once i click on this add record button it will be there so this entry is here now in order to test whether it's actually permanent let's close the application and let's run it again because that's why that way we can check if the entry is actually permanent and there we are so the entry is permanent now you can see this is the line where we see the name the email address and then we have two buttons here so this one is added and we can update a record and this one is delete and we can delete a record so first let's update a record and here i'm going to change the name to let's say dot uk and then i press update and let's see okay it's updated here and now let's close the application again just to see if that actually has a permanent effect and there we are so it does have a permanent effect here and we can add more records it cannot be blank so let's just enter something in here add this record and you can see we are using a very basic functionality here we don't check whether the email address is a correct email address from its format perspective and so forth but we're just entering stuff in there okay so you can see now we have this list of elements and if we enter a bunch of entries in here it's going to still work perfectly so let me add even more just so that we have enough entries actually i need to name here so just we have enough entries so that we can scroll here so you can see this is a list view here at the bottom which is a recycle view to be more precise and we have a bunch of entries now we can delete them and this screen pops up so you can see i usually want to delete it i'm going to say yes and it's gone so you can see we have two different types of dialogues one is for the confirmation when we delete something and the other one is in order to edit a record all right so update a record updating it you can see record updated so a bunch of code surrounding the actual fundamentals of sql lite that i want to teach you but i still think that this is a great demo that you can use whenever you need the code because this was just plainly the crude operations crude standing for create read update and delete and these are the things that you are going to require throughout your career at least if you want to be a developer right because you will need to do these things regularly that's why this demo will be amazing for your purposes you will just be able to reuse the code from here and apply it for your examples so let's look at the surrounding things first before we look into the database handler and into how the database stuff itself works therefore we need to look at the layout first so we can see the layout is generally a toolbar and an include so include is content main and in the content main that's where we do all of this name row so then the actual text field for the name a text view and then edit text so text view add a text text view is just the visual visible text to speak and the edit text is the actual field which you can edit the text that and then we have an add record button or just a button which says add record and once we click that well it will add something to the list which then will be updating the list view that we have here at the bottom which is a recycler view all right so next thing is the items from let's look at the individual items that we have here so we have a tv name so text view name then just a text view then give it a proper name it's just an empty space in between then the text view which is just a colon very basic then tv email which is the email address and then we have this ib edit which stands for image view edit and image view delete so if you want to see it in code well you can download it but you can see we have three text views then we have an image view and another image view which contain these beautiful little buttons and they have ids so that we can talk to them by a code then we have this dialog update which is our custom dialog which we use in order to update the record there we have simply the update record text then again a text view and a text field or an edit text so to speak and then here we have a linear layout where update and cancel are all the way up to the right you can see it's tv so text views and that's this is the code well you can download the code anyways right but what's interesting here is that we are using linear layout and the gravity is towards end and as far as i know we have not used end yet which just means that we position stuff towards the right okay and of course this is not this field here but it's actually when we added these two text fields which are positioned towards the right that's what happens via gravity end all right the next thing that we should look at is the item adapter so here we are using an item adapter and you should check out the adapter or in general the recyclerview video the recyclerview demo if you want to understand better what's going on here but basically we are inheriting from as a recyclerview adapter and we're creating an item adapter which requires a context and an array list of a class which i called emp model class we're going to look at that later on and then we have the oncreateviewholder which we overwrite and here we just inflate the individual item row then we have the onbindviewholder where we bind every individual line with the correct name and email which means every single entry that we have here will be updated at least the name and email will be updated and you can also see that the colors are changing so every second one is gray every second one is white that's what happens with these two lines of code then we have this holder with set on click listener so we have inside of our holder which is this one here the view holder now if you want to know more about the view holder you can check out the class maybe we should do that now first so we have this view holder which inherits from viewholder of recyclerview and in there i have five properties or variables so to speak and one of them is the linear layout main the text view name text to email text to edit and iv delete so linear layout main is going to be the whole row here all well that is the linear layout with the whole row and as you can see we have a textview name takes your email takes your edit button and text you delete button and all of them are inside of this view holder and well we need them in order to access those so if i want to for example change the text of one of the items well i can see we can read the item name and actually change our view there our text view name text the same goes for the email address then for the color well um the changing them and then we have the iv edit which is our image view edit once we click on that which just means this button here once we click on this button we want to call the context update record dialog item and inside of context well what is context context is main activity so inside of main activity we have this method called update record dialog and we will look at that later on because here a bunch of stuff is going on so that's why i want to do it in the next video then we have iv delete which simply calls the delete record alert dialog and that will then delete it and of course as always we overwrite the get item count so it's the general approach that we have seen in the demo as well there's not much difference except for that we're talking to multiple items now instead of just one item within our list element so within an individual list item but other than that this recycler view is pretty much the same all right so this was the preparation and i would say in the next video we are going to check out our main activity our emp model class which is very basic and our database handler which is not so basic that's why i'd say we cut it down into multiple videos so see you in the next video and make sure that you understand the stuff that i explained in this video because this is really just repeating what we have learned so far because it will make things easier once we actually go to the next video so see you there quick pause so you're learning something about android in this video and i hope you enjoy it if you want to learn everything that you need to know to become a real android developer then definitely check out my android master class because in this course you're going to build a bunch of great applications along your journey to becoming an android developer first you're going to learn the kotlin basics then you're going to learn to build one app after another and while you do that you get a bunch of demos which will really dig deep into the concepts as well as presentations which will help you to understand what you're learning so don't miss out and get the course right now you can find the link in the description below now that you understand the basic structure of this demo the sql lite demo let's look into the sql part of it because we actually want to use it later on and you will need to do it in many applications each time that you want to store something permanently on your device then it really makes sense to use this okay use sql lite because it's a lightweight super powerful tool in order to store data permanently so the first thing that i did is i created a model class so a data model class which is a very generic thing you can see here it's just a class called emp model class so emp standing for employee and what it contains is an id a name and an email and the id is an integer name is a string email is a string and we're going to use that class model because the idea behind this data model class is it doesn't have any methods it's really just super simple it's really just saying okay if we create an object of this class then it should contain these three values and properties so to speak okay so what we can go next to is our database handler so i have this other class here called database handler and that one is taking care of the whole database stuff so we have this database handler which is just our own way of implementing a sql lite open helper so this is the sqlite openhelper class which we inherit from this is a class that exists in android and we just go ahead and create our own version of it we adjust it to our needs and you could see you need a context a database a factory which can be null and a database version when creating such an object now the companion object is something that we use in order to create constants and especially the main idea is to create static variables which are available throughout the application and we make them constant by using this const keyword so it's values and we use these capital letters this is an indication that something is constant even though the database version might change but it will only change when we develop it so we as a developer are the only one changing it it's not going to change during runtime that's why it is okay to leave it as a constant now what is the database version well a database version is the version of the database and when you want to change the database meaning that you want to add another column to the database then you would also have to update the database so you have to change the version of the database so in our example in our database we only have three entries one of them being the id we don't see the id in our ui the other one being the name and the third one being the email now let's say we would like to add another column so a fourth column being the age if we add another column to the database we also need to update the code in our code here and we need to let our application know that we are using a new database version because otherwise we could break the data because it's expecting only an id a name and an email but it's suddenly also getting another value which it doesn't know what to do with so it's important to version your databases okay then we have the database name and that is the overall name and within a database you can have a bunch of tables and we are just using one table for now so the database is the whole thing containing potentially a bunch of tables thousands of them if necessary and we are using one table for now which is the table called contacts so we use employee table as the table name here and then we have a private const value key underscore id and this one has an underscore underneath it when giving it a name what this indicates or what we use this for is as the primary key now what is a primary key you might ask well it's a primary keyword is a key in a relationship data rational database that is unique for each record this means that there is no way that an id so the same number so to speak will appear twice or even more times each of the ids is unique so when we look at our entry again so in our database here so we have an entry this is entry number one it will probably have the id 0 we can start with any value that we want but it makes sense to start with zero so the first entry will have an id of zero the second entry will have an id of one this one will have an id of two and this one will have an ideal three okay now in order to make it a little simpler let's change those names actually let's change the name to this one being zero then this one being one this one being two and then the next entry being three okay so i'm trying to keep it super simple here in this example what i want to do with this is to demonstrate you the uniqueness of this key of this primary key so let's say we have this entry zero which is tested you test dot uk and this is our id 0 then the next entry is id 1 and this one is id 2 and so forth so each time we create a new entry it will get a new id and each id will be unique okay so let's say i delete this entry here the entry with the id one it doesn't mean that once i create the next entry it will automatically be id one and it will fill the gap so to speak and it doesn't also mean that this two will suddenly become number one and just everything will be pushed to the top now that's not what happens each every single object that is created so every entry in our database that is created will get the next id so the next one that was ever used so if we had 13 entries at one point and even if we deleted 10 of them the next entry that will come will then have an id of 13 or 14 whatever you want to start with so if we start with 0 then it would be 13 because we have 13 and trees before starting at 0 so the latest id number will be 12 so the next entry would then be 13. okay so i hope this didn't confuse you but just memorize this a primary key is a unique identifier which means it only exists once if you want to know more about this whole stuff then definitely check out google on that i don't want to go too deep into databases and how they work on a super deep level all right so that's the main thing that you should remember unique identifier so the key id is unique then coming back to our code so we have also the name and the email for our user now let's see what oncreate does well oncreate is called and we had oncreate before but we had it for activities but oncreate also exists for databases so it will be called once the database is created so what we do here is we say we want to create a contacts table so we create a new table and we create the table like this so you can see create table table context then opening bracket key id as the integer primary key with the key name called text and the key email called text and you can see that this is so to speak a super long string and a lot of things could go wrong here so you have to be very very precise when writing such a string because this is an actual sql instruction and it generally works like this when you want to create a new table you start with a keyword called create table then you have an empty space then you have the name of the table that you want to use and i use the table context name which means employee table and it's important that there is no empty space neither at the beginning nor at the end because we do the empty spaces in here already if they are required okay then the next one is we open the bracket and in there we have the key id which is our underscore id empty space integer which indicates what type it is and what's important and databases the data types are not the same as they are in kotlin or in other programming languages so integer it doesn't say int as we would do in kotlin it actually is integer then there is this keyword primary key and that's important so we have empty space primary empty space key by this we say that this key id will be our primary key then we add a comma we add the next column name so to speak because this is the first column name then comes the second column name which is our key name and it is of type text and then we have the third column name which is again separated by comma which is our text here so which is the keem email so written in a normal language without all of these different variables that we're using what is written here is create a table called employee table and then in brackets you add all of the columns so column number one is and called id it's of type integer and it's going to be our primary key so this is going to be unique then we say name texts which is our the second column and it's called name and it's going to be of type text then we have email which is of type text so this will be our table that we create so now when we create this table it will have these columns it will have the column underscore id it will have the column name and it will have the column email and now if you create an entry id will be automatically incremented so it will start at zero and each time that you create a new entry it will automatically increment the id and it will create a new entry so now let's say we create a third entry here and we call this one frank the tank and we have the his email which is frank at the tank dot com and now we decide to get rid of this guy or let's say rid of marie curie who invented or found out that there was a lot of radiation coming around or floating around so now let's say we delete marie curie and now we create a new entry and it will again be the next id so it will not start or not will not be put here on the two but will actually go to the next value so now would be four the next integer so to speak and so forth so that's what this whole primary key stuff means and that's also what we're doing with this table here our create table okay so now once we created or prepared an sql command which this is so this here is a sql command we can execute it so what we're doing is in oncreate we say please go ahead execute a sql command which is this command that we stored in create context table of course you could have passed this whole thing directly in here but then it would be super difficult to read and i think it's a little easier to read it like this by the way if you want to know more about the data types in sql you should definitely check out sqlite.org and here sqlite version 3 there you will see that there is null well empty we've seen that integer real has a floating point value then text is just some simple text and blob has the value is a blob of data stored exactly as it was input so this is something that is then transferred into a data type which is something that we have seen when we created an image out of our drawing you might remember in the drawing app where we drew an image on the view and then we created a png out of it well while we did that we had that data floating around in a byte sized or in a byte format so to speak and that's what pretty much blob allows as well so that for example if you have a png then it will store that as a png if you have it as a excel file then it will store it as an excel file and so forth so if you want to know more about it you should check it out here you can also see the limitations of those for example if you want to store date times then you can use it as a text using the iso 8601 so you could store a date like this or you could use it as a real value or as an integer so you have a bunch of options here by the way here boolean data types are stored as zero for false and one for true so there is no need for an additional data type just for that all right so that's what happens in the oncreate now i'd say this video is long enough and i wasn't even expecting for this to be done within one video so i'd say let's go to the next video where we're going to look at on upgrade and all of the methods that i created myself in order to make this database work so see you in the next one quick pause so you're learning something about android in this video and i hope you enjoy it if you want to learn everything that you need to know to become a real android developer then definitely check out my android master class because in this course you're going to build a bunch of great applications along your journey to becoming an android developer first you're going to learn the kotlin basics then you're going to learn to build one app after another and while you do that you get a bunch of demos which will really dig deep into the concepts as well as presentations which will help you to understand what you're learning so don't miss out and get the course right now you can find the link in the description below welcome back in the last video we saw how to create a table in our database which was this create context table how i called it and it's just this sql command that we are executing in this line and the command reads like this so what it does it just creates a table with three columns of which one being the key which is the id and that will be the primary key which will auto increment all right so then we have this on upgrade method which is called when we upgrade our table which means for example if we add another column and then we want to change the way our database table looks like then we would need to let our app know that we are now working with a new version of the database because now it's not having three columns anymore but it also has a fourth column for example the age and then we would need to update it so for example if we go ahead and say okay i want to have a key called h in here as well then i would need to update my database version but i'm not going to do that here i'm going to keep it simple for now so the next thing is our fun ad employee so this is a method that is not inside of the standard sqliteopenhelper class but its code that is created by us so custom code and what we have here is that we want to add an employee so how do we add an employee well we want to have a emp which stands for employee model class object and it will return a long so what does it do well first of all we need to have a database and in order to get the database that we want to write on we can use this dot writeable database so a database that we can write on and this meaning in the context of in which we are and we are in the context of our database helper then we need a bunch of content values that we want to put onto this database so what we do is we create this content values variable which we then put the values in so for example i put the key name and the emp name into this content values container so to speak so you could imagine it like a container and you fill it up with all of the information that you have and once you're done with filling it up you add that to the database so what i'm saying is please go ahead and add a new entry for the name and the following email and then insert that into the database and we store the result in this variable called success so success will return a long value so here a long value so this db insert with this method we say please insert into the table called table context with the null column hack being null so we don't pass any value here has the content values there please so just write whatever we had in the content values onto the table so just add a new entry a new line so to speak in this table called table context and then we close this database because we opened it at one point we started to write into it then we need to close it we need to close the connection and then we return the success value so if it worked or it didn't work but it's not a boolean it's actually a long because we are returning along here and well because insert returns along so if you look at it it's a java method as you can see here public long insert so it returns a long and if things go wrong it will return -1 and otherwise it will return the insert with conflict so if you want to know more about it you would need to go deeper and see what this method does and so forth but generally speaking what we do with this is we just store that in the table and if things go wrong then the return value will not be positive but it will be -1 okay so this is how we can add data to our database with this approach as you can see here so we need to get the database then we create an object of content values we put the column information that we want to add so name and email as you can see there is no id i'm not explicitly adding the id even though it's part of our table here key id and that is because it's not necessary because the id is a primary key and it will auto increment each time that we add a new entry it will automatically increase by one so how do we read data from the table well i have this view employee method and what i get is an arraylist of emp model class object as a result of it so that will be returned from it and that's the cool thing because we don't need to go ahead and create maps and use maps here but we just have our own model that we created which is this emp model class where we just have the three names of the columns so if we want to have more names in here we could go ahead and create a new value called h which is of type integer for example of course if we do so we also need to be careful and need to put that into our table description which is in our database handler but basically you can see we can use this class emp model class so this model in order to use it very quickly so we had that here before when when adding an employee we also just pass in a whole object of emp model class so we don't need to specifically add in a name then an email and so forth but we're just passing the whole object and now it's the same thing here we are when we're viewing the employees returning or getting a bunch of imp model class objects so a bunch of employees so then we go ahead and we create a new value an employee list which is of type arraylist which contains employee model class objects so here emp model class object and then we use the select keyword so what we're doing here is we are running a select query with a select query we can select data from a table so we are saying here select star or asterisk from this table called table context so what we say is just give me everything that you have in this table all the rows that are in the table all the columns give me all the data that is in there so that's what our select query does here and then we need the database again and this time it's not a writable database but we just need the readable database and here is something that we have to use when running through database so we need a cursor so the cursor it starts at null or it is null at the start and then what we try is we try to fill the cursor with a raw query which is going to run the select query this one onto our database and we said that we don't want to have any specific selection arguments that we also pass in here so then what we're just trying is to execute our query here and if that didn't work with this raw query then we just say okay execute a sql query and that will then select the sql query so we have this try we try this code and if it doesn't work so something goes wrong then we catch the code we run the execute sql with the select query so select star from table context and then we return an array list here all right the next thing i do is i take this var id which is of type integer so i create the variables here for the different columns the id of type integer the name of type string and the email of type string so the same formats that we had before and also the same that we have here in our employee model class and then i move through the cursor so here i tried to get the cursor so to set the cursor so to speak and then i move it through the cursor and i have a while loop here which moves to the next entry as long as there are next entries so going back to our example here in our table it starts here at this element and then it moves through the cursor just goes through the whole table and every single time that it does that what we want to have is we want to get the id from the entry of the cursor so from the table so to speak we want to name and we want the email address and then we want to create an employee object out of that so an employee model class object out of the information that we read from the table and we store that in a new list here where in our employee list so we add that to our employee list which is this array list of employee model class objects and once all of that is done we'll return this employee list so our function which was supposed to return an arraylist of employee model classes is going to do that and here by the way this cursor has a function for getint which will get the cursor's column index at the key id then the column index at the key name and the column index at the key email so coming back to our table here let's say we are at this cursor row then for the id it's going to check this and it's going to store it in here then it checks the column name key name so it goes here to name where with frank einstein and then we have the email bam email frank einstein dot com okay so that is what it's going to do and then it's going to store them in emp and we store it in our list and return that list okay so that's how you can read from a list so we're viewing the employee list in our example now from the crude operations we had so far create and read so what is left well the update and the delete functionality so let's look at the update functionality here again if you want to update it's the same as if you're writing so if you're creating something so we're using the same approach we use a database object which is a writable database then we use contentvalue here that can put values object in order to put the data that we want to put in there and then we use the update method in the table context content values where clause key id is equal to emp id and then you have somewhere argument so what does that mean let's look at this update employee once again what we're saying is please when you call this method give us the employee object that you want to update and i will return an integer once you've done that so the employee object we look at that we get its name we get its email and we change those values but then we also need to know at which id we need to update it so here in our example at which id do you want to update the name so do you want to update it for example for our three where you now change rank to tank to frank the gank then you could do that by updating this whole object and passing that object but you need to know the id because maybe there is another frank the tank so this is not the only frank the tank out there maybe there is another entry for frank the tank so we have this employee frank the tank twice in our company for some reason whatever reason that might be maybe an error maybe not and his email is even the same so he's using the same email address or maybe we don't have the correct email address of him or something like that and now we would like to update that but then we need to know which of the ids we need to change because just knowing we need to update frank tank is not going to cut it because we have ranked the tank twice so we need to have his id in order to update the correct one and that's where this where clause comes in so we say the key id should be the employee id that is passed to us so this employee object that is passed to the update method when we call it okay so that's what we update and then we close the database and then we return success which is of type integer all right and now the final entry here is delete employee so how do we delete an employee well again we need to say which employee we want to delete so which employee object we want to delete we have again a writable database so we are changing something in the database and each time you want to change something in the database you need to have a writable database if you just want to read something in the database then a readable database is enough but otherwise you need a writable database so for creating updating and deleting you will need a writable database and for reading you will need a readable database so here you have the same concept you have the content values but now you just say which id you want to delete so content values put key id id so now we know which content value we need to update so we run our database delete method this time we need to say in which table we want to delete something then we need to say at which key id we want to delete something and we say at the employee id and the where arguments we set that to null again so we don't pass any additional information there then we close the database and we return whatever success was so success doesn't necessarily mean that it was successful or not but it's just what we are returning here which is of type integer all right so that's our database handler and we looked into it in a lot of depth but now let's go to the main activity and in order to do so i'd say we make a little break and we come back with a lot of new energy and force and will in order to finalize this example because as you know this is a pretty advanced example that we have here and we are going to require that for future projects so see you in the next video quick pause so you're learning something about android in this video and i hope you enjoy it if you want to learn everything that you need to know to become a real android developer then definitely check out my android master class because in this course you're going to build a bunch of great applications along your journey to becoming an android developer first you're going to learn the kotlin basics then you're going to learn to build one app after another and while you do that you get a bunch of demos which will really dig deep into the concepts as well as presentations which will help you to understand what you're learning so don't miss out and get the course right now you can find the link in the description below welcome back to the fourth part of the sequel light series and in this video i would like to go over the main activity in which everything comes together so everything that we prepared so far the database handler the model class and the item adapter and of course all of the xml files that come together in this main activity dot kt file so first of all we of course have our on create then we have this button add set on click listener which adds a new record and then we have this setup list of data into recyclerview all right let's look at this add record first add record is a function that i created in here and what it has it takes it the name from the edit text so it takes this name here that one from this edit text then it takes the email from this email edittext and puts them into variables and then we have a database handler this database handler is the object of the database handler class which we created so this one here so we create an object of that class in order to access its functionality all right we pass in the context because if we look at it it needed a context when we wanted to create an object of it so that's what we're doing here we're passing in this this is the context which means this is our main activity then we check if those variables aren't empty so those two edit fields are the texts they may not be empty because if they are empty then we will write a little toast saying name or email cannot be blank let's try it add record and then name or email cannot be blank okay so that is that now let's look what happens if it's not empty so if we enter something into both edit text then we create this val status which uses the add employee method of our database handler so of our database handler class and we pass in an object of emp model class zero name email so here if we look at add all right if we look at this method it's easier like this we will see that here it expects an emp or employee model class object and then what it does is it inserts that entry into the table context okay so it pretty much just adds it to the database that's exactly what we wanted right now you might wonder because here it says zero and zero i told you about the id which is this primary key it will be automatically incremented so the thing is even though i enter 0 here for employee model class the system itself will take care of assigning the right id so this will automatically get the right id because it will be automatically incremented okay and we will see that in a second we will see that actually on the device then we have the check here if the status is greater than zero then things worked out because i told you earlier if the status was minus one then that means that we get an error or that we have an error and something went wrong when trying to add something to the database so if things went well what we're going to do is just write a little toast here saved record saved for a long time and displaying that what i also do is i clear the two text fields so to edit text so once i enter something here someone someone's email and here it's simone so i wrote it incorrectly but once i do that you can see the two edit texts are cleared and that is done by dot clear by this clear method all right and then we can see this setup list of data into recyclerview again so what is this method because we're using it twice right we used it already here in the oncreate method and we do it once again once we stored something so once we added the new entry well now let's look at this function because it's used to show the list of insert data so it's pretty much updating the list that we see here at the bottom so this list of all the entries that we have in our database so we check is there an entry in the database or not and we do that by checking get items list so let's check this out get items list gets an arraylist of employee model class so we use our database handler and then we get the employee list so what this will do is it will just return database handler view employee now of course you could sum that up because that's what the ide tells me so let me do that it's a one-liner so it's a little shorter it got rid of the whole line so here basically we're just returning the view employee that's what this get items list function does and view employee as you might recall it selects all employees from our table contacts so it will return all the entries that we have in the database okay so that's what get item list does let's go back to the function though so to the setup list of data recyclerview so we checked are there any entries if there are some entries then go into this code here so then do something if there are no entries then go into this code so let's look at this code because it's shorter first so what happens is we say that the rv items list visibility is to set set to view gone what that means is that this rv items list so basically this list here is made invisible and instead this text here no records available is set to visible so tv no records available visibility view visible so i just want to indicate that there is no entry to the user if there is actually no entry and we can see that once we delete all of those entries we will see that this other text comes up but before i do that i want to show you the entries in the database actually okay now let's look at this we set the rv item list to visible we made this other thing which says there is no entry to gone so we make it invisible and then we create a linear layout and we create this item adapter which gets the entries of our database via get items list and then it puts them into the rv items list so we're just filling the adapter with the data that we have from the database and this is the method that is called just when we open up the application so that's why it's called in the oncreate method here and it's also called when we add another entry it will also be called if we delete an mg so add record we looked at that already now let's look at update record dialog and we can see here that what we're doing is we go and create a new dialog and by the way this is just going to create this dialog here so edit and you can see update record so it's basically creating this thing here so we create a new dialog with a certain theme so we have this style here which are defined theme overlay app compact dialog so it has a certain width and height so it doesn't take just the full available height and so forth so these are details but let's look at this set cancelable we set that to false then we set the content view to dialog update and we looked at that before it's this dialog here update record name edit text and so forth and then we set the text to whatever was in emp model class so that's why it's important then if we want to update something we need to pass in the object that we want to edit then the same goes for our email id so basically what we're doing is when we click on edit we want to know what we want to edit so we want to populate the values here the 0 and here this tester test uk we want it to be an entry otherwise we don't know what we are updating that's why it's important to have it here that's exactly what we populate here with those two lines so we are getting the value from the model so from our object and we are setting the text to our edit text update email id and it is within our update dialog which is this object that we created of type dialog which uses our our layout dialog all right and then we create an set on click listener so there are two buttons here and when we edit the update button at the cancel button so when we update we take the names and the email so those two entries we take those two entries and we store them in variables then we create another object of our database handler here create an object of it and then we check if those values aren't empty so what i'm doing here is not is empty but you can sum it up to is not empty so maybe that's a little better this way so we say if the name is not empty and the email is not empty because if one of them is empty then we don't want to update it so if both armed them aren't empty then call this method update employee and store the status so if it worked or not inside of this variable status of course we need to pass in an emp model class with the name and the email where do they come from well they come from here so the values that we put into this these two fields and then we of course also need to pass in an id so we're just taking the id of the element that we wanted to edit anyway so the idea id of this record that we're currently updating so in this case let's say this is at id 0 then this is the entry that we are editing all right and then if the status is greater than -1 we know that it worked so we make a little toast here saying record updated we update the list so we update whatever we have here in this list here and then we dismiss the update dialog so we get rid of this dialog here this one okay and well if we didn't fill all both elements so if we didn't fill both of those edit texts then we get this toast here and then what shall happen if we press cancel on this dialog this button here well we just dismissed the dialog and then of course we may not forget to update dialog show because we are preparing everything up to that point up to here that's where the preparation of the update dialog happens and this is where it actually is appearing or where it's actually shown all right and then the delete record dialog so that's the dialog that pops up once we try to delete something so this one here are you sure you want to delete zero so here you can see even in the text it's using the name of the entry and it can say no and yes now let's look at this we create another builder and that's something that we have done when we used alert dialogues maybe you can remember if not then check out the alert dialogue video once again so we use the builder then we set the title we set a message so title message the icon here set icon and then we say what should happen if we press on the positive button we give it the name or text so we say yes here and here we create another database handler object we have the status again so we check if deleting an employee and specifically the employee that we are passing in works so what we're doing is we give in or pass and the id of the employee model class object and we set name and email to empty then we check the status again so is the status nut or greater minus one because then everything worked out so we just let the user know and we update the data into recyclerview so let's say we update this list here so let me delete something you can see this list here is updated and that's due to this call here because if we don't call this then the recycle view will not know that it has to be updated and of course once we click on something we want to dismiss the dialog interface and the same goes if we press no then we also want to dislist dislister or dismiss the dialog interface dismiss and then we need to of course create that alert dialog we can set the cancelable to false or true and then we finally show this alert dialog okay so this is the code now let's look into how you can actually debug so let's say you want to see all the entries in your database because here i'm displaying them with a bunch of code but what if you just want to see something on your database of the phone what you can do is you can go to your build gradle and here it's important go into the module app and here inside of the dependency so you need to scroll down a little bit you go inside of the dependencies and here you find this debug implementation im.dino dot db inspector colon 3.4.1 at aar so that is what you need to enter there because if you do so and you run the application what will happen is you will find this application here so let me drag this up you will find this sqlite demo application which looks like a little database icon okay once again it's this icon right here okay when you open it you will find some database entries so on my device i can see here com sql lite demo databases employee database so you can basically see it uses the folder entry data user 0. nothing too fancy here and then it uses the name of my package so com sqlite demo slash databases and then slash the name of the database and it's called employee database so it's called like i have defined right here so database name that's what i've defined here so if i want to see database name where it's used it's actually used up here okay so that's what we're passing to the sqlite open helper and then we set that to employee database so employee database and then we have this employee table by the way you can see that there is something that we didn't create ourselves this android underscore metadata so this is another one that is created but that's not something that we have done it's automatically there but there is this version one that you can see here employee data table v1 v1 standing for database version one so whatever we entered here and then there is this table called employee table so that's exactly the name of the table that we have defined here and now let's look at this table so now you can see in this table we have an underscore id and you can see there is 1 six seven eight nine and two three four five they are all missing so that's these are the ones that i have deleted and then you can see at zero or at one actually at id one you have the name zero and the email test test uk now let's look at our application once again so let's move over and you will see that's maybe it's a little small but you can see here zero test at you test uk then the entry 37 so name three and email seven and then here's simone at someone's email okay so all of these entries are there now let's delete this one and say yes and let's move over to our database connection you can see suddenly it's gone so it has been deleted of our database so let's change this three to hello for example let's move over edit this three to hello update now let's go over to the database tool and you can see again here it says hello so this is a way how you can debug your entries that you created in your database in your sql database and yeah now i think we make quite some progress and you can use sqlite now and it is a long processor there's a lot of stuff going on when using sql lite but the thing is once you've set it up and you went through the process once and then maybe a second time it will really become natural after a while because it really is the same structure over and over again the only thing that changes are the table names the columns that you want to have in there and then where at which point you want to make those entries but that's pretty much it okay so i hope you enjoyed this little journey of just using sql lite and i know it's been a long one but no worries it's going to be fine we're going to go and use this because then it will become a lot clearer once we use this in another real world example and we're going to use it actually in multiple real world examples so we're going to use it in the seven minute workout app and also in the happy places app alrighty so you made it all the way to the end of the video thank you very much for still being with me i'm very happy that you made it all the way through and that you learned something about sqlite and i hope you can now use it to put it into your applications and apply it there and build even better apps i wish you all the best on that journey and of course don't forget to check out my android master class because there you're going to learn how to build applications using exactly what you just learned and a lot more so really you will become a real android developer okay so thanks a lot for being with me once again don't forget to hit that like button if you haven't and also that subscribe button because we're uploading android content once a week on fridays and also don't forget to check out one of those two videos here
Info
Channel: tutorialsEU
Views: 55,978
Rating: undefined out of 5
Keywords: Tutorial, Android SQLite Tutorial - SQLite In Depth Guide - SQLite Database Tutorial, android studio sqlite, sqlite android studio, android studio sqlite database, android studio sqlite database tutorial 2020, sqlite tutorial 2020, sqlite android, sqlite android studio kotlin, kotlin android sqlite, databases android studio, kotlin sqlite tutorial, kotlin sqlite database, android SQLite create table, android SQLite select, android SQLite delete row, sqlite in depth guide
Id: CzGNaiSoh7E
Channel Id: undefined
Length: 65min 3sec (3903 seconds)
Published: Fri Aug 21 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.