Now that you understand the basic structure of this demo, the sequel demo, let's look into the sequel
part of it, because we actually want to use it later on and you will need it in many applications.
Each time that you want to store something permanently on your device, then it really makes sense to
use this.
I can use Sequel Light because it's a lightweight, super powerful tool in order to store data permanently.
So the first thing that I did is I created a model class or a data model class, which is a very generic
thing you can see here.
It's just a class called MPLX model class.
So in standing for employee and what it contains is an ID, a name and an email.
And the idea is an integer name is a string as a string.
And we're going to use that class model because the idea behind this data model class is it doesn't
have any methods.
It's really just super simple.
It's really just saying, OK, if we create an object of this class, then it should contain these three
values and properties, so to speak.
OK, so what we can go next to is our database center.
So I have this other class here called Database Handler and that that one is taking care of the whole
database stuff.
So we have this database handler, which is just our own way of implementing a sequel Light Open Helper.
So this is the sequel that opened Helper Class, which we inherited from.
This is a class that exists in Android and we just go ahead and create our own version of it.
We adjust it to our needs.
And you could see you need a context, a database, a factory which can be now and a database version
one creating such an object.
Now, the companion object is something that we use in order to create constants.
And especially the main idea is to create static variables which are available throughout the application.
And we make them content by using this key word.
So it's values and we use these capital letters.
This is an indication that something is constant, even though the database version might change, but
it will only change when we develop it.
So we as a developer are the only one changing it.
It's not going to change during runtime.
That's why it is OK to leave it as a constant.
Now, what is the database version?
Well, a database version is the version of the database.
And when you want to change the database, meaning that you want to add another column to the database,
then you would also have to update the database.
So you have to change the version of the database.
So in our example, in our database, we only have three entries, one of them being the ID.
We don't see the ID in our UI, the other one being the name and the third one being the email.
Now, let's say we would like to add another column.
So a fourth column being the age.
If we add another column to the database, we also need to update the code in our code here and we need
to let our application know that we are using a new database version because otherwise we could break
the data because it's expecting only an ID, a name and an email.
But it's certainly also getting another value, which it doesn't know what to do with.
So it's important to version your.
Databases.
OK, then we have a database name, and that is the overall name, and within a database you can have
a bunch of tables and we are just using one table for now.
So the database is the whole thing containing potentially a bunch of tables, thousands of them if necessary.
And we are using one table for now, which is the table called contacts.
So we use employee table, has the table name here, and then we have a private const value key underscore
ID and this one has an underscore.
But underneath it, when giving it the name, what this indicates or what we use this for is as the
primary key.
Now what is a primary key, you might ask?
Well, it's a primary key word is a key in a relationship date relational database that is unique for
each record.
This means that there is no way that an ID, so the same number, so to speak, will appear twice or
even more times.
Each of the IDs is unique.
So when we look at our entry again, so in our database here, so we have an entry, does this entry
number one, it will probably have the ID zero.
We can start with any value that we want, but it makes sense to start with zero.
So the first entry will have an idea of zero.
The second entry will have an idea of one.
This one will have an idea of two and this one will have an idea of three.
OK, now, in order to make it a little simpler, let's change those names, actually.
Let's change the name to this one being zero.
Then this one being.
One.
This one being, too.
And then the next entry being three.
OK, so I'm trying to keep it super simple here and this example, what I want to do with this is to
demonstrate you the uniqueness of this key, of this primary key.
So let's say we have this entry zero, which is tested you to test the duck and this is our I.D. zero,
then the next entries, ID one at this one is I.D. two and so forth.
So each time we create a new entry, it will get a new ID and each ID will be unique.
OK, so let's say I delete this entry here, the entry with the ID one.
It doesn't mean that once I create the next entry it will automatically be ID one and it will fill the
gap, so to speak.
And it doesn't also mean that this too will suddenly become number one and just everything will be pushed
to the top.
Now, that's not what happens each every single object that is created.
So every entry in our database that is created will get the next I.D. So the next one that was ever
used.
So if we had 13 entries at one point and even if we deleted 10 of them, the next entry that will come
will then have an ID of 13 or 14, whatever you want to start with.
So if we start with zero, then it would be 13 because we have 13 entries before starting at zero.
So the latest I.D. number will be 12.
So the next entry would then be 13.
OK, so I hope this didn't confuse you, but just memorize this, a primary key is a unique identifier,
which means it only exists once.
If you want to know more about this whole stuff, then definitely check out Google one that I don't
want to go too deep into and databases and how they work on a super deep level.
All right, so that's the main thing that you should remember, unique identifier, so that kid is unique.
Then coming back to our code, so we have also the name and that email for our user.
Now let's see what our.
Well, uncreate is called.
And we had uncreate before, but we had it for activities.
But uncreate also exists for databases.
So it will be called once the database is created.
So what we do here is we say we want to create a contact table.
So we create a new table and we create a table like this.
So you can see create table table contexts.
Then opening bracket key ID has the integer primary key with the key name called text and a key email
called text.
And you can see that this is, so to speak, a super long string and a lot of things could go wrong
here.
So you have to be very, very precise when writing such a string, because this is an actual school
construction and it generally works like this.
When you want to create a new table, you start with a keyword called create table.
Then you have an empty space, then you have the name of the table that you want to use.
And I used to table context name, which means employee table.
And it's important that there is no empty space, neither at the beginning nor at the end, because
we do the empty spaces in here already if they are required.
OK, then the next one is we open the bracket and in there we had the key ID, which is our underscore
ID and the space integer, which indicates what type it is and what's important.
And database's the data types are not the same as they are in Scotland or in other programming languages.
So integer it doesn't say int as we do in cotton it actually is integer.
Then there is this key word, primary key and that's important.
So we have empty space primary, empty space key.
But as we say that this key ID will be our primary key, then we are comma, we add the next column
name, so to speak, because this is the first column name.
Then comes the second column name, which is our key name and it is of type text.
And then we have the third column name, which is again separated by comma, which is our text here.
So which is it?
Email.
So written in normal language without all of these.
Different variables that we are using, what is written here is create a table called employee table
and then in brackets you add all of the columns so called number one is called ID.
It's of type integer, and it's going to be our primary key.
So this is going to be unique.
Then we say name text, which is our second column, and it's called Name, and it's going to be of
typed text.
Then we have email.
Which is of type text, so this will be our table that would create.
So now when we create this table, it will have these columns where they have a column on the score,
it will have the column name and it will have the column email.
And now if you create an entry, it will be automatically incremented.
So it will start at zero.
And each time that you create a new entry, it will automatically increment the ID and it will create
a new entry.
So now let's say we create a third entry here and we call this one Frank the Tank.
And.
We have his e-mail, which is Frank at the tank dot com, and now we decide to get rid of this guy or
let's say rid of Marie Curie.
Paul.
Invented or found out that there was a lot of radiation going around floating around.
So now let's say we delete Mary Curie and now we create a new entry and it will again be the next ID.
So it will not start or it will not be put here on to two, but will actually go to the next value.
So it now would be for the next InTech or so to speak and so forth.
So that's what this whole.
Primary key stuff means.
And that's also what we're doing with this table here, Hakodate table.
OK.
So now once we created We're Prepared and sequel Camonte, which this is.
So this here is a sequel, Camonte.
We can execute it.
So what we're doing is in uncreate, we say, please go ahead, execute a sequel command, which is
this command that we stored and create context table.
Of course, you could have passed this whole thing directly in here, but then it would be super difficult
to read it.
And I think it's a little easier to read it like this.
By the way, if you want to know more about the data types in school, you should definitely check out
school like dot org and here Scollard version three there you will see that there is no, well, empty.
We've seen that integer real as a floating point value.
Then text is just some simple text and blob as the value is a blob of data stored exactly as it was
input.
So this is something that is then transferred into a data type, which is something that we have seen
when we created an image out of our drawing.
You might remember in the drawing app where we drew an image on The View and then we created a page
out of it.
Well, while we did that, we had that data floating around in a bite sized in a bite format, so to
speak.
And that's what pretty much allows as well.
So that, for example, if you have a PMG, then it will store that as a PMG.
If you have it as a Excel file, then it was stored as an Excel file and so forth.
So if you want to know more about it, you should check it out here.
You can also see the limitations of those.
For example, if you want to store data times, then you can use it as a text using the ISO eight,
six or one.
So you could store a date like this or you could use it as a real value or as an integer.
So you have a bunch of options here.
By the way, here, boolean data types are stored as zero for false and one for true.
So there is no need for an additional data type just for that.
All right.
So that's what happens in the uncreate.
Now, I'd say this video is long enough, and I wasn't even expecting for this to be done within one
video.
So I'd say, let's go to the next video where we're going to look at an upgrade and all of the methods
that I created myself in order to make this database work.
So see you in the next one.