Welcome back.
The last video we saw how to create a table in our database, which was to create context table, how
I called it, and it's just this simple 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 also 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, it would go ahead and say, OK, 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 at employees.
So this is a method that is not inside of the standard.
Sequel, light open helper class, but it's 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 MPLX, which stands for employee model class object when 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 writable 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 Perabo, which we then put the values in.
So, for example, I put the key name and your pimp name into this content values.
Containers, 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 inserted into the database and we saw 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 no column hack being now, so we don't pass any value here past the content values there,
please.
So just write whatever we had and 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, 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 had returns a long and if things go wrong, it will return minus one, 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 as we just store that in the table and if things go wrong,
then the return value will not be positive, but it will be minus one.
OK, 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 idea, even though it's part of our table here, key ID.
And that is because it's not necessary, because there is a primary key and it will also 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 array list of ICMP 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 empty middle 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
in model class, so this model.
In order to use it very quickly, so we had that here before winning on adding an apology, we also
just.
Passing the whole object of middle class, so we don't need to specifically add in a name, then an
email and so forth, we're just passing the whole object.
And it's the same thing here we are when reviewing the employees, returning or getting a bunch of empty
middle class objects.
So a bunch of employees.
So we go ahead and we create a new value and employee list, which is of type A list which contains
employee model, class object.
So here, MP Malphrus object, and then we used to select keywords.
So what we're doing here is we are running a select query with a select 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 the writable database, but we just need a readable database.
And here is something that we have to use when running through a database.
So we need a cursor.
So the cursor it starts at now or it is now 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 query,
then we just say, OK, execute a single query and that would then select the square.
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 cycle
with the select query.
So select staff from table context and then we return and the rails 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 idea of type integer, the name of type
string and the type string.
So the same format that we had before and also the same that we have here in our employee model plus.
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 do while loop here, which moves to the next entry.
As long as there are next entries, so going back to our example here.
And our table, it starts here at this parliament and then it moves through, the cursor just goes through
the whole table.
And every single time that it does, that's 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, when tonight we want to email address and then we want to create an
employee object out of that.
So an employee more class object out of the information that we read from the table and we store that
in a new list here or in our employee list.
So we add that to our employee list, which is this array list of employee small class objects.
And once all of that is done, we return this employee list, so our function, which was supposed to
return and the list of employee morale classes, is going to do that.
And year, by the way, this cursor has a function for that end which will get to cursus column index
had the key ID.
Then the call index at the key name and column index at the key email, so coming back to our table
here, let's say we are at this curser row then for the idiots going to check this and it's going to
store it.
And here.
Then he checks the column name he name, so it goes here to name where with Frankenstein and then we
have the email spam, email Frankenstein dot com.
OK, so that is what is going to do.
And then it's going to store them and Empey and we started in our list and we turn that list.
OK, so that's how you can read from a list.
So we're viewing the Ambrogio 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 lead 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 are creating something so we're using the same approach.
We use a database object, which is a veritable database.
Then we use content value here to use objects in order to put the data that we want to put in there.
And then we use to update method and the table context, content values.
Workless kiddie's.
Is equal to MPLX idee.
And then you have somewhere argument.
So what does that mean?
Let's look at this.
Update employees 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 you've done that.
So the NPG 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 ideas we need to update it, so.
Here in our example.
Which I did, do you want to update the name?
So do you want to update it, for example, for our three, where you now change Frank Tatang to Frank
the gang, 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 I 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, the tank is not going to cut it because we have Frank to think twice.
So we need to have his ID in order to update the correct one.
And that's where this workless comes in.
So we say the kid should be the employee ID that is passed to us.
So this.
Employee object to this past two to update method when we call it.
OK, so that's what we update and then we close the database and then we return success, which is of
type integer.
All right.
Now, the final entry here is the lead employees out the how do we need 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 veritable database.
If you just want to read something in the database, in a readable database is enough.
But otherwise you need a writable database.
So for creating, updating and deleting, you will need a virtual 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 it.
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 where arguments we start 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 and 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.