Welcome back and this video, we are going to add the functionality that stores that we have finished
and exercise, so once we're done with the seven minute workout, it should be stored in the school
database.
And in order to do so, we are going to use to school that open helper, which we're going to build
up ourselves, of course, as we have done in the demo that you hopefully have watched, because we're
going to need quite some of the features that we have discussed their.
All right, so let's get started.
Let's create a new cutline file, which will be a new Scotland class, and I'm going to call this one
fescue �lite open helper.
And here I'm using the.
Small Q here, that's it, and this should inherit from the sequel that open helper class.
All right, now, of course, we need to have a bunch of additional information and I'm going to store
them in a companion object.
So basically an object which allows us to create static variables and the static variables that I want
to use are going to be the following one.
So I'm just going to paste them in here because there's no use in me typing everything in.
We need to have the database version.
We need to have the database name.
Then the table that we want to use or the name of the table, and I call it history, by the way, the
database name is going to be seven minutes work out dot DB, the column number one and number two.
And the number one is going to be the ID.
And I add an underscore because this is going to be my primary key and then the second column will be
the completed date.
So when did we completed when did we finish the exercise?
And that will be stored in this column.
And then we, of course, need to add the constructor here, so we will pass in the context then the
database name the factory and finally the database version.
So database version.
Now, what is this context and factory?
Well, we get them from our school, that open helper, so when we create a new school, that open object.
So one of our own open helper, then what I want to do is I want to have a context which will be of
type context.
Then I need the factory, which will be a qualified database.
Curser factory now here we need to import context and the cursor factory and the cursor factory will
be a nullable, so either we pass one in or we don't.
Now, you can also see that our school open helper is still not happy because we haven't implemented
the members that are required.
So I'm going to mark all of them, which means both.
And I'm going to press OK, and it will automatically generate a code for me here.
All right.
In the uncreate method, that's where I want to create the table.
So I want to create our new exercise table and I'm going to execute the following code.
So first of all, I create this variable called create exercise table.
Which will just have the string that should be executed, so pretty much the sequel Light Coat.
So here I'm saying create a table, create table A. Space plus the table name.
I actually like this table.
History is the name of the table then.
The opening bracket.
The first column name, and that was called Maidique, and then.
The type that we want to use for it, so I'm going to use to type integer, so the idea should be an
integer and at the same time it should be our primary key.
Then I added comma and the second column.
So the second column is our column completed date and then.
I need to also tell of which type that should be, so it should be of type text.
I'm going to put all of that into brackets, so I'm going to open a bracket here and close the bracket
here, so that's going to be the string that should be executed once we have our database.
So I'm going to execute the sequel Light Code, which will be create exercise table.
Now, DB, as you can see, is an nullable.
So I'm just going to add a question mark here.
So if it works.
So if there is a database, then please execute the code to secure line code.
Otherwise don't do anything.
It's just going to return now or just going to be null.
So basically, this is the sequel Liko that will be executed, create table history, integer primary
key, complete data text.
All right, now we need to take care of an upgrade and in the upgrade method, what we want to do is
we want to call TV, execute a sequel.
And the sequel that I want to execute is going to be to drop the table if it exists.
And what is the name of the table that I want to drop?
Well, it's going to be our table history.
And again, our DB is a nullable, so I'm going to add the question mark here, I'm going to make a
safe call here and then uncreate should also be called with this database.
So basically, I'm dropping the table and then I'm recreating it with the latest version, so to speak.
So when I upgrade the version of my database, for example, if I want to have another column in the
future, then the database will be in a different version.
And I need to.
And that's what an upgrade does.
All right.
Now we need to have the function which will take care of actually adding something to the database.
So I'm going to create a new function called add date, and it should get a date when it's called,
which is going to be of type string.
All right.
So the parameter is going to be date and then.
I create a new content values object, so this allows me to add a bunch of values to put them into this
content values object and then write them to the database.
So I'm going to say values to put so we add a piece of entry or a piece of detail into it.
And the key should be our column completed date and the value itself should be the date.
So I want to store the date, whatever is passed inside of our column completed date, and then I need
to write that.
So I'm going to create a new database, which will be this writable database.
So we create a new writable database object here.
This allows us to read and write to the database, so read from and write to and then we can go ahead
and insert something to the database.
And what do we want to insert?
Well, first of all, we need to pass in the table on which we want to insert something, then the null
column HECH and we can also just pass a null here and then the actual values, which would be of type
content values.
And I'm just going to pass on the values that we prepared here.
So here we just prepare everything.
Actually in these two lines, we prepare the values that we want to pass and in our case it's very basic.
We just want to put in the date itself so we're not adding any additional column information.
So we only have this one column, which is relevant because the other one is the I.D., which is our
primary key.
Right.
So it's just going to increment each time that the new entry is made.
And then, of course, we need to close the database because keeping it open is not a good idea.
All right.
So that's our ad date function and now we can use that in our Finnish activity.
So once we finished the exercise, so the seven minute workout, then we want to add that to the database.
So pretty much the moment.
So the second or the minute, whatever the exercise was finished, that's what we want to store permanently
in the database in order to do so.
What we need to do is we need to create a bunch of code inside of the uncreate method.
So here that's the uncreate of our Finnish activity because I want to execute the code as soon as the
Finnish activity is opened and created.
But of course, I don't want to put all the code in here.
I'm just going to create a new function which will take care of that.
So let's create a new private function, private fund ad date to data base and so I'm going to call
it, and then in there I need to get the calendar.
So calendar, I'm going to create a new calendar object which will use the calendar and get its instant's.
So we just go ahead and create a calendar object.
And from that, we can get the daytime so, well, daytime time, it's going to be calendar dot time.
And that's pretty cool because now we will get to the current date and time of the system and then we
can use that in order to log it.
So let's lock that.
And I'm just going to use an E, which is going to be an error warning, so to speak, even though anything
else would be fine as well, actually.
And information is fine as well.
So I'm going to use the tag.
Date and the message should be our date time.
All right, so that's our first log entry.
And then we want to format into a certain format.
I want to put that date into a certain format.
So in order to do that, I need to create a new object of type as the F means simple date format.
And here I need to import simple date format.
And I'm going to use.
The Java version, so here, let's look at the imports Java, the text, simple data format.
And what I need to pass in here is, first of all, the pattern that I want to use and I'm going to
use the pattern day, day, month, month, month in capital letters, which means if it's January,
it's going to be J and then the year.
The hours, the minutes and seconds.
And then local thought get default, so it's going to get the default of the local, whatever the default
format is, and then we can go ahead.
And get the date from that, so VUL date is going to be STF thought formate.
And that's formatted data time into a date, so it's just going to take the date time in which format
ever head then is going to use this format that we prepared for this pattern.
And it's going to make a new date with this format out of the date time that we just have gotten, which
was the current time, whatever this method is called.
And then we need to have a database handler which will take care of storing that data into a database.
So I will create an object of my skill like open helper and then need to present the context, which
will be this and the factory.
And I'm just going to pass a null here.
And there's, of course, only works if I were at the very top here.
Our factory is of type curser factory nullable.
And then we can use this database handler, which is an object of our skill wide open help.
Right.
And just call it a date function and we can pass in the date.
So if we look at it, this was our function at date.
So we now passing the date and then we just go ahead and write it to the database.
All right, and here we can also add a log entry, so I'm just going to say date is going to be added
now in order to test it.
We, of course, need to call this function.
And as I said, we want to do that inside of our uncreate of the Finnish activity.
So that's pretty much it, right?
Let's go ahead and test that.
And by the way, what I prepared is, of course, I decreased the rest time of duration and exercise
time, duration to one second because otherwise it would take forever to go through this exercise here.
All right, now we are.
So we are in the finished exercise.
Now let's see if the Lockette found anything.
Now let's search for a date and was with capital letters and you can see is January the 16th.
That's Thursday.
And the date was added.
All right.
So that worked.
Now, of course, we will only see if the entry was made, if we either add something to our dependencies
here in our gradle, as you've seen before, or if we just go ahead with the next video where we are
going to see how we can add that to our recycle of you.
So we need to create the respect of you for our Finnish activity.
And then we, of course, need to call the database and get the data from that database.
OK, so that's what we're going to do in the next two videos and then we will be done with this whole
application.
So see you in the next one.