SQLite Database Tutorial Android Studio | Insert, Delete, Update and View Data in SQLite Database

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi guys welcome to all coding tutorials today in this video i'm gonna talk about using sql database for building android apps i will show you how can we create a simple app in which we can insert update and delete the user information and also read the data in your android app so let's begin first launch the android studio select the first option start a new android studio project and then select the empty activity now give a name to your project i'm writing here sqlite application then you need to write a package name as per your choice mypack enjoyment is all coding tutorial click finish so the main activity.java has loaded just increase the space let us go to activity underscore main.xml let's go to the text let us change the layout to relatively out here and now what we will do we will delete these constraints we don't need them then we will change it to match parent text i will change to please enter the details below so this is just a text to show like a title to our app exercise let us make it 24 dp then i will provide some margin top margin we'll give a 20 dp let's give an id here for our text view id slash text title and then what we will do we will create an edit text width will be matched parent height will be wrap content close it here and i will give an id here id slash name and then we can show a hint so hint equal to name and text size also again here we will keep it slightly bigger 24 db then it should be layout below so we have to define a layout below id slash text title which is our text view it will appear below our text view then the input type here we will define text person name because this is the box we are creating to enter person name and copy this edit text and paste it down below so then again i will create one more edit text paste it again here so now let us change the ids first to the secondary text id i will change it contact then hint i will change it contact and it should be below id slash name and input type since it's a contact so i will change it number then the third edit text box we will change the id as a dob for date of birth then hint i will write date of birth keep the text size same and it should be layout below contact so just change it to contact and input type also we will change it to similarly number so three edit text has been created uh let us give some padding here adding equal to 10 dp this will create a padding from all the sides in our layout look at the design now so you can see three edit text boxes with the hints have appeared nicely now let us create some buttons here so we will create a button layout width will be match parent and height will be wrap content close it here and id will be id slash button insert this is our first button to basically insert the data into the database then text size again here i will keep it 24 tp and text text on the button i will show insert new data and then i will give some margin see for all the buttons so this will be the first margin so 30 dp and it should be below the layout below we will define as id dob id slash dob then let us copy this button entire button and create two more buttons and then button then idea will give a button update this will be a button for updating the data so write the text update data we'll delete this margin and it should be below but let us see what is the id it is button insert so i will will keep this i will keep this below our button insert lets copy and paste here and then in our third button id we will change it to button delete so this button will be for deleting the data from our sql database so the text we have here we will write delete existing data then margin will delete and it should be layout below button update so we will change here then let us create another button and paste code for the button here again and id let us change it button view and the text on the button will change its view data so this is the button when the first when the user will click on that and it will show all the entries from our sqlite database and define layout below button delete and now let us go to the design so this four button have come let's go to mainactivity.java here so now we'll start creating variables ready text so we'll create name comma contact comma dob so these are our three edit text variable then we have button insert comma update comma delete comma view so we have four buttons here we have created four buttons so now what we'll do now we'll create a java class that is dbe helper just name it db helper okay click ok and then here we will extend this to sqlite open helper and here we are here we have to show we have to implement the method so select both and you can see both the methods oncreate and on upgrade method it has been created now create a constructor select only first one and all these arguments will delete you don't need that and this nullable also we will delete so we have only the context context now inside the super we will write our name of the database user data is our name of the database then null factory null and version let us write one so what it does it creates a database with the version one then we will change it here the db will change it to db here and then here also db then we will write a sqlite query db dot exact sql so it will execute the escalate query and write create table user details so this what it will do it will create a table naming user detail and inside that the columns will be named text it will be our primary key usually we give a primary key as id but let us keep it like this here contact also text then comma dob text so we have three columns the type text that here on upgrade we will write db.exec sql so again we will write an sql query here so drop table if exists user details one thing to note here is that the database name is to be unique in our db helper class then we will create some methods here public boolean method insert user data so this is the method for insertion of the data into our sql database into our table the table name is user details so we will pass the strings here string name string contact string dob now let us write sql database db this dot get writable database it gets a writable database and and pass it to db then we write content values content values equal to new content values so basically this will create a object content values which you can use to put values inside our table so content values dot put the string name and we will write comma name so name is our column name and it should match with our column names please be careful and then second column is our contact so content value dot put contact contact and then change it here content values dot put dob comma dob so we have copied the contents put and pasted it twice and then write long result equal to db dot insert so here we are using the insert method the insert method requires three parameters first parameter is your table name user details second parameter is column hack we will just leave it in optional and then third parameter is content value so result equal to -1 that means if the insertion has failed it will return false else it will return a true which will be used in our main java code now copy this entire code and create another method name of the method we will give update user data the parameters will be same because we need all these parameters let us delete the first content value we only need to update the contact and dob given the username i will change it db.insert i will change it insert to update dblot update method and the update requires parameters first parameter is user details second parameter is content values the third parameter is a where clause basically so we'll write name equal to question mark and the fourth parameter is the where argument so we have to check that where clause string is matching with the where argument string so we will write new string brackets in the inside the curly brackets name [Music] now here we will create a cursor object cursor db dot rock query cursor is like selecting the row much like excel you might have worked in so when you select some row that whatever is selected gets loaded into the cursor object so we'll write select star from user details where you have to write entire sql query here where name equal to question mark comma and then we have to write where argument so basically new string brackets inside the curly brackets right name so basically it will check whether the name matches with the given name in the function we have passed the string name so if our cursor has some data we'll write if cursor.getcount greater than 0 that means if cursor has some data then only these things should happen then only update should happen else if the cursor does not have any data then return false why we are writing this because if we write only update method uh what it will do it will always return true if even if there is a null value then again copy and paste the entire method here and change it to delete data so now we will delete as per the user name so whenever user gives his username that entry particular entry will be deleted so we don't need to put any content here and let us keep the cursor here again and there is some error here i think there is some mistake in the curly bracket we are not closed here let us close it here and now it is okay so here again cursor db it is checking select from user detail where name equal to name and then here inside the content values will delete delete the argument then we will change it to db dot delete so it requires only three arguments user details where clause and the where argument and again it returns only true and false and now we will create one more method we'll copy this delete method and paste it below so we'll change its boolean to cursor this will be basically returning the data basically cursor type and then we will write to get data basically it is for getting the a particular row then this where clause we will just delete because you don't need that and we don't need any argument because we don't need a particular specific data however this can be used to get the particular data if you write a where clause we don't need this entire if clause and let us write here again now because here depending on our selection return cursor this is our get data method is also complete so now we have created all our methods now go back to main activity.java so let us initialize these variables name find v by id r dot id dot name and then we'll write contact equal to find view by id r dot id dot contact dob equal to find view by rt r dot id dot dob is for date of birth initialize our buttons insert equal to find view by id r dot id dot button insert semicolon now update equal to find view by id r dot id dot button update then delete equal to find view by id r dot id dot button delete then view equal to find view by id r dot id dot button view then let's define let's create a db helper variable dp and initialize that here db equal to new db helper context is this now create a listener for our insert button insert dot set on click listener new view dot on click listener now under the on click method we will store the values in string so write string name text txt equal to name dot get text dot tostring then write string contact text equals name equals equals contact dot get text dot tostring and string dob text equals dob dot get text dot tostring semicolon now we will write boolean check insert data so we are defining a boolean variable here db dot now our function is insert user data it requires three strings so we will write the strings name name text contact text and dob text then we'll write if check insert data equals to true so if the insert user data has returned true we will show toast message that new entry inserted else if it has returned false so that means the entry could not be inserted we will write new entry not inserted so this is our first listener then let us copy this and paste below this semicolon and change the name insert to update this is basically our update button so the listener we need the three strings and just change it here update user data db dot update user data it again ask for three strings so in the toast message we can change entry updated if it returns true and if it is written if it return false we will write entry not updated and here boolean we will change it to check update data because our function is for update and then here again we'll copy this and inside the if clause we will write check update data equals to true so that's how we create in our update listener update button and then copy here and paste again now we will create our delete button so delete we'll change it to delete and we don't need these we only need first string because we delete by giving the name so we'll change it to delete data our method is deleted and delete these two strings now boolean we will name it check delete data and paste here we will write here entry deleted the toast message will change and deleted and entry not deleted so this is how we create our delete button then at last we will create our view button view dot set on click listener new view dot on click listener then write cursor res for result equal to db dot get data so basically we have created a method in dbhelper which returns a cursor type so we write here if result dot get come equal to zero that means there is no data selected so we show a toast message no entry exists so if there is no data it will show a toast message now entry exists and we write return now we will there is if get count is not equal to zero we will write string buffer buffer equal to we'll define a buffer here new string buffer now we create a while loop so basically as long as the row values are red so res dot move to next brackets inside that we will buffer we'll write buffer dot append so in the first column name it will store in the buffer name column and after the codes we can write plus res dot get string value 0 string error is 0 and stores in the buffer then just write a slash in here and we'll copy and paste twice here and we'll change the second buffer dot append contact and we'll change string number one and then the third we will write date of birth and string number two we'll create two slash on here basically to create a space for the next row so for each row it creates a kind of a buffer and now we will show an alert dialog dot builder so basically it will be a alert message which will pop up so alert dialog dot builder builder equal to new alert dialog dot builder menu to reload this and we will write builder dot set cancelable true then we'll write builder dot set title so the title of our alert dialog will be user entries and then we'll write builder dot set message so here our message which is stored in our buffer buffer dot to string then we'll let builder dot show so this what it will do it will show the alert message and all the values which we have stored now let us run the project everything is complete so here i have mirrored my phone and yeah you can see there are three edit text boxes come and four buttons are there so first let us write name user one contact i will write one two three four five one two three four five and date of birth i will write zero one zero one twenty twenty insert new data you can see the message new entry inserted now just view that and you will see the title user entries and all the three values name equal to the user one contract has appeared then let us make a new entry user two and what i will do it will change the date of birth here so 0 2 0 to 2020 insert new data you can see an inventory inserted and this view so you can see two entries here so two rows it has shown then let us change again let us update that user two entries let us see whether update method works so when we click insert new entry not inserted because it already exists then we update data then it can update so you can see in user 2 the date of birth is now updated so the new value has come now we'll see how delete works or delete existing user data so user 2 is it is showing the message that i enter deleted and you can see the second entry is deleted and now only there is a first entry and now if we try to delete with user 2 it cannot delete it is showing the message not deleted because user 2 does not exist now if we update new entry not updated because that does not exist so this way we can do insertion updation delete and view data get all the data now let us see the data in our where is it is lying in our database go to the right bottom and device file explorer click there and you can see your android device name go to the data data and select your package name here so this is my package name here then open that expand that and see database and you can see the database userdata.db is there now save the data to your pc i'm saving it in the over my e drive and then i will open the db browser for sqlite open the database so i will say where it is where i have saved it user data.db and then right click on the browse table and you can see the entries name contact and dob user one contract and dob has come so this way you can view the database stored in your android device so that's it friend so i hope this tutorial you found it useful please like and don't forget to subscribe our channel we'll bring more videos like this and thank you very much
Info
Channel: CodingTutorials
Views: 329,995
Rating: undefined out of 5
Keywords: View Data in SQlite, SQlite Tutorial Android Studio, update data in Sqlite, SQlite Database tutorial, update data in sqlite database in android, insert update delete select data in sqlite android, Update and View Data in SQlite Database in Android Studio, store user data in SQLite, sqlite database in android, insert update delete select data sqlite android, android sqlite database tutorial, store data into sqlite database android, select data from sqlite database android
Id: 9t8VVWebRFM
Channel Id: undefined
Length: 28min 23sec (1703 seconds)
Published: Wed Jul 01 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.