SQLite + Android - Create Database Schema (Book Library App) | Part 1

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hello there and welcome to my new video so with this video we're going to start SQLite with Android tutorial series and then we're going to create a beautiful book application in which we are going to create a SQLite database with a recycler view as well so you will see how that will look like okay so now we're going to start by creating a layouts for our application so first let's remove this textview from our main activity layout and we're going to add a recycler view here okay so just click OK and Android studio will automatically add a dependency in our Gradle build file for this recycle view so let's check it out as you can see this line of code is automatically added so here we have a warning so we need to click this build project and it will refresh our layout and now we can see our recycler view clearly ok so let's connect those constraints with a parent okay and now we need to add the ID for our recycler view because we're going to use that inside our main activity class so let's add ID we'll just name this recycler view and now we're going to create we're going to add one vector file so here we're going to select one image from the Android studio let's search for ADD and we're going to choose this one so let's change the name of this add icon and let's change the color to white okay just click Next and finish so now we're going to add the floating action button here okay and we're going to select this add icon ok just like ok and Android studio will again add a dependency for a material design you can see this is automatically added for us so here we have another warning just above saying that we have added the vector file inside our application and we need to add this line of code now vector drawables dot you support library equals to true inside our Gradle build file so let's do that let's open up and let's remove this warning okay just place this here and pick sync now and this warning should disappear all right so here on our floating action button we need to add one more attribute here and let's set that to true okay now let's connect right and bottom constraint of our floating action button to a parent okay and those two constraints should have margin of 40 so let's check it out let's run our application and see how will that work okay I think we should decrease that value a little bit so let's set to 28 maybe right then bottom and let's run application now and now it looks better so next we need to change this action bar title so inside Android manifest file we need to add attribute the label and let's set it to a book library so when we run our application our activity will have a book library title okay so that's good next we're going to start by creating a new Java class and this Java class will represent our SQLite database scheme and here we're going to create all the methods for deleting adding and updating our database so this class will extend the SQLite open capital class and we need to implement two methods on create and on upgrade okay so next let's create a constructor for this class as well alright so as you can see our constructor has many of our parameters here but we're going to remove all of them and we're going to keep just this context and from the super we're going to add the name of our table and the version so let's declare those variables here so the first let's add a context next we're going to add a public static final string so this is basically a constant and let's name this database name as you can see it's all uppercase letters okay our database should be called the book library DB DB is extension for a database next we need to add the database version and this type this time it will be integer type we will say this one next is the table name our table should be named something like my library like this okay so next we're going to add the columns four-hour-a-day for our database table so we're going to have a four different cones the first one will be ID next one will be title a next one author and the last one will be number of pages for our book of course so the cone ID will be will be auto increment you will see later but for now it's stay that way so let's copy this database name variable and let's paste this inside our super method and database version here as well so we can change those access modifiers from public to private but we will going to do that later so now inside on create method here we're going to start writing some code first we're going to create SQL query but before we create one SQL query I want to show you I want to show you the SQL schema for our table so this is the SQLite statement SQL statement for our table and basically we have a create table a keyword then the name of our table which is my library then we have inside the brackets we are declaring a columns for our table the first one is ID the type integer and primary key then auto increment so our ID will auto increment every time we insert a new data inside our table so after auto increment we have a comma and in space and then a book title this is the second column a text type and we have a column again then we have a OMA again and then we have a book author text type and then comma again and book pages and type of integer this time so after that we're closing the the brackets and we have a semicolon at the end so it's very important right this SQL statement properly you need to use a space every time so you need to remember that and now we're going to go to Android studio and create one okay so here we're going to create a string type named query and here we're going to start by creating our SQL statement so first as you saw we need to set create table then space then table name then again we need to use a concatenation because this is Java it's kind of boring but you know you need to write that so we're opening brackets but before brackets we set the space then call an ID then integer primary key Auto increment then a comma then space plus next one is title column title then plus then space text then comma and then space name plus next one is cone author then + space text comma space next is cone pages space integer space and here we're going to close our query so close the brackets and semicolon okay so let's check it out once again if our query is properly formatted so let's execute this query by setting execute SQL method so as you can see now our text has changed the color a little bit so we need to set this space but just before the integer we forgot to do that so it is very important to write this query properly otherwise you might get runtime error okay so in our own upgrade method we're going to create another query so let's use dB dot X acute SQL and this time SQL query will be simple as drop table if exists and space then plus table name and after that we're going to call our own create method so whenever we operate at the table we need to call on create method as well so let's change those mode access modifiers to private okay so now we have created our my database helper class okay and the next we can set the ID for our floating button here and let's create a recyclerview object here and floating action button so let's find the idea of those two elements inside on create method okay and let's add a set onclicklistener for our floating button and this onclick listener will lead us to next activity so let's create one so activity ant activity and this one will be named add activity so in this activity we're going to add our items inside the database and our table of course okay so here let's called intent so new intent paths main activity dot this and add activity dot class and let's start this activity and let's run our emulator to see if that works fine so click this plus and there we go it works fine for now alright so let's open up Android manifest file and let's set the parent activity name to main activity and are able to add the book okay so now when we run our application and navigate to the second activity we're going to see this back arrow and of course the title so we can hit this back button and get back to our main activity all right so that works fine as well [Music] next thing let's create with this second activity which is add activity so we need the three edit text for title author and pages okay let's connect the constraint for this first one top the left and right and let's say the top margin to 150 and let's do the same for our other to edit text let's connect the constraints so top constraint should be around 16 for those last two edit text and let's add the IDS for those three elements the first one will be a named a title input so we're going to remove this text attribute and add a hint attribute with which we'll say a book title okay for the secondary text we're going to remove this text and add hint with book author and the ID to author input and a last one will be named at pages or number of pages okay and ID pages input okay so next we need to add one button of course and add the padding to this constraint layout so we can have a little better UI okay so let's say this button top margin should be around 40 I think and let's set this ID to not say but add button okay so button text should be just simple as ad let's change the text all caps to false layout height to 70 DP and layout width to match the parent okay so next thing we're going to set the layout width for our three edit text to match the parent and let's set those constraints to zero left and right okay and let's run application to see how will that look [Music] okay let's click this floating button and there we go so it looks fine it is just a simple UI we don't need anything more okay and the third one is a type of number so I think we should add the text size to this button so let's set to 20 DP SP sorry maybe and let's set the style text style to a bold so let's see how will that work it doesn't look that good I will change that okay and I don't know I think I should get back to the previous so let's remove this text style and and that's it so that will be all for this video we have created two activities and in the next video we are going to add logic and all the functionality for adding our book inside the database and that will be all for this video please like this video if you find it helpful of course and see the next one
Info
Channel: Stevdza-San
Views: 377,742
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
Id: hJPk50p7xwA
Channel Id: undefined
Length: 14min 44sec (884 seconds)
Published: Wed Feb 05 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.