SQLite + Android - Insert Data in Database Table (Book Library App) | Part 2

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello there and welcome back to my new video so in this video we're going to continue from the last time so in the last video we have designed that two activities and also we have created my database helper class in which we have defined our SQLite database scheme so as you can see this is our main activity we have our floating button and this is our add activity and we have a three edit text and one button and this is our my database helper class which extends SQLite open helper class and here we have defined our database scheme as you can see we have on create and on ambulate method here as well those two methods are override it and okay it looks fine everything works perfect here so now let's run our application okay so now we're going to implement the functionality here inside our add the book activity and to do that we're going to define a new method here inside our my database helper class so let's create a new method here we're going to name this add the book okay so first what we're going to do here we're going to create SQLite database object okay so just name this DB and we'll use a keyword that this so this will refer to our SQLite open helper class and we're going to use its method get writable database so as you can see we are referring to this as collide to open helper class and we're using its method get writable database so we can write to our table next we're going to create the content values object and basically in these content values we are going to store all our data from our application and we're going to pass to our database table so we're going to use its method name the put okay so the first parameter is key and the key will be a call name and the value will be data so the first key will be cone title so we don't need to use a column ID because the column ID will be incremented automatically and the second parameter will be titles so let's add three parameters inside our method for title author and pages so our third variable should be type of integer because our pay a number of pages is integer type okay all right so next we're going to copy these two more times and we're going to set the column author and the third one to column pages okay and next we're going to use our SQLite database object and we're going to insert the data inside our database table so the first parameter should be table name so let's refer to our table name from the ball next the second parameter should be now in the third parameter content values so those are the values which we have imported as you can see all right so this method will be used inside our add activity so as you can see those are the values for our variables okay and next we're going to before that we're going to set store this result from our insert method inside this result variable and we're going to say if this result is equals to minus 1 that means our application of failed to insert the data so it's the first parameter for our toast message we're going to use a context object from our constructor okay and here we're going to set for example failed just a simple message and in else block we're going to say success so we can get some feedback from our application yeah if our task is successfully done or not okay and now we can we can call our method from add activity so let's head over there and before we do that that we need to create objects for our UI elements here so we have three at the text and one button so let's add them here the first one is a title input the second one the author input and a third one pages input and the next one is a button of course add button and here inside on create method we are going to find the idea of those elements of course and we are going to set on click listener for our button and inside that on click listener we are going to call our method from our my database helper class okay so let's create my database helper object here and let's initialize our class so we need to pass one parameter here which is a contest and let's call this at the book method so we need to pass three parameters here and we're basically going to get the text from our three edit text and we're going to convert those to string and then we're going to pass that to our add the book method which will then process our data and insert into our SQLite database okay so the third parameter is the type of integer so we need to convert our string to integer okay and now I think it will work fine so okay I forgot to add this order in pages here inside their content values so I'm going to do that now and we can run our application to check how it works so let's bring this protonation button and let's write to just a simple values here or here like title one then author one and for number of pages address type for example 100 so click Add and as you can see there is a successful message so that means that our data is successfully inserted inside our database so in order to see that we need to download this SQLite browser application so click this download and I have downloaded already this no installer version so we are going to open this application and open our database from Android studio so let's run this just find that excel file here DB browser for SQLite okay so before we open this database we need to export our database from our Android emulator so in order to do that it's very easy don't worry just like this view and then to windows and then we have a device file explorer here so click that and on the right side you will see this file manager so select this your device on which you are running your application then select data directory then data directory again and here we are going to see all the application you have on your device so my name of our project here this is called SQLite tutorial and here we have a database directory so we can open up that and there are three files so we need to copy all those three files and save them on the desktop so just right click on them and click save as and the just export them on the desktop okay so let's open up our database browser and click this open database navigate to our desktop and select this book library DB DB DB is extension for the database as you can see we have here my library table so it actually exists and it contains the four different columns like ID book title the book author and book pages okay so so that's a good thing so we have actually created our database and our table so let's check the data inside our table so click this browser browse data in select table and as you can see we have our title one author one and the 100 and those are the values which we insert it from our Android application so let's add one more let's set the one more book for example title to order 2 and for example 110 so let's click click add and now from our from let's close this database and the let's export once more again but before that click right click and click synchronize so it will refresh order all the files in your file manager and then save them inside the our desktop directory okay so let's open up our database again click open and let's select our table and as you can see we have our second row and our ID Row is incrementing by itself automatically so that would be it for this video we have successfully created our table and inserted some data you saw how that works and in the next video we are going to display those data inside our recycler view and so thanks for watching please like this video if you wanted help of course and see the next one
Info
Channel: Stevdza-San
Views: 124,168
Rating: undefined out of 5
Keywords: sql, sqlite, android, database, data, app, android studio, how to, guide, tutorial, sqliteopenhelper, sqlitedatabase, create, read, update, delete, crud, operations, insert, add, write, table, schema, put, query, recyclerview, recycler, view
Id: RGzblJuat1M
Channel Id: undefined
Length: 9min 54sec (594 seconds)
Published: Sun Feb 09 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.