All right, guys, welcome back.
Let us set up our SQLite database so that journey has a starting with our model.
And so remember that the model represents the data.
And when you start talking about relational databases are, well, data stores almost in general, the
model really is just going to be converted into a table or serialized into JSON or whatever.
So our model is a good starting point for what our data table needs to look like.
So our data table would have the ideas given to us by this entity.
It will have the MC, the model and the VIN in the forms of string values.
Right?
So we're going to go to our solution Explorer and go to New Get Package Manager, and then we're going
to look for a library called Sequel Lite Dot net dash net dash pico.
And that is our sequel, right, with our dot net wrapper, our own sequel, the sequel that was originally
written in C++.
So this is just a wrapper.
So it can work in our dot net environment.
So let's go ahead and install that.
And after we get that one, we're going to install Sequel II, PCL, raw dot provided dot dynamic CDL
CDC.
So that one is kind of a mouthful.
So you can just hit pause and make sure you get the name right and install that.
And then once that is completed, what we'll have to do is go over to our model and we'll have to decorate
it now with things that will help skylight to appreciate what this model is.
So when I say things, I mean attributes like table.
So right above the class we're going to define table control DOT and include any missing using reference.
Actually, I think that's a wrong librarian.
Let me try that again because we're dealing with Scala it's right there we go using sequel.
Right.
So table and then we have to give the table a name.
In this case, I would call it like cars.
All right.
Naming conventions may differ.
Some person's plurals, their table versus leave.
It's the same name I plurals or so cars.
And then what we do here is well, in the base entity.
First of all, I need to let this base entity know that the idea is going to be the primary key.
So I have to annotate this with primary key at the missing using reference.
There we go.
And I would also just tell it to auto increment, right?
So proper databases that proper is relative, but in databases and you typically want if you have an
ID primary key or an integer primary key column, you let it be an identity column, which means it
will automatically increment for each new record.
Right?
So we have that for the I.D. and then for our other columns.
We could add other constraints.
I could see that, well, make is unique or not.
So let's make Vin unique because no two no two vehicles should have the same Vin.
I think I said that from the start.
Right.
So I could actually select Maxlength.
So let's say Maxlength for the VIN is probably 12 digits, 13 digits, whatever the validation would
say.
And I want to make sure that it has that unique constraint.
Right.
So you could actually kind of change them a long comma, separate them, or you could just add multiple
in each line, right?
Whichever one you think is neater.
Obviously that wouldn't work because I have the tool.
So let me do that.
And then that's right.
So you could do it this way or it could change them along.
Like what we did here, we just comma separate them.
So you just fitted our model to know that it should be some representation of the database, right?
So let's jump over to our service.
So remember that our service is what is really taxiing the data for us.
So I'm going to remove this static list because well, we don't want to return a static list in our
service.
I'm going to also create a field I'm going to call this field C Collide Connection content, right?
A private sequel, eight connection, our connection.
And I'm going to have another method here that I'm just going to call init in it and in, in it what
we'll do is define the connection.
Oh, I'm sorry.
This should be void.
So private void in it.
There we go.
And then we have check.
Is the connection not equal to null?
If we're already connected, then you don't need to connect.
Otherwise we want the connection to be equal to a new C.
Nixon.
And we want to connect to a table.
I will call that car.
All right.
So let us jump over to our car service class.
And I've already written the code, so I'll just walk you through.
If anything, all you need right now is the class, so you can actually.
Ah, well, we already had a method that was just returning the new list.
So if anything, you can, you know, everything that was in that method and I'll walk you through exactly
what needs to happen in this particular class file.
Right.
So we have car service.
We have a new field that we're calling on and it is of type C Collide connection.
Of course, you know, you need the missing using statement for SQLite and then we have string DB pass.
Right.
So obviously you can choose to decorate it as private or not.
Right.
I'll just on to create the connection one for uniformity.
So we have sqlite and then we have the DB path.
Then we have a constructor.
So keyboard for shortcut for constructor is right c, t or r and pressed up twice.
It generates a constructor.
This constructor is going to take in a DB path which are able to configure.
So we're injecting a DB pass and then we're initializing our local field to have the value that was
injected in.
Next we have a method called init.
Now in it, in it we this is a void and all we're doing is one checking if the connection has been made.
So if a connection has been made, we don't need to make another connection, right?
So if it's if it's not equal to null, then we return.
Otherwise go ahead and c con is equal to new C collide connection and we take the DB path.
I also want to see create the table that is car, right?
So if it doesn't already exist, we want to create it now.
Car of course it knows that when it creates it, yes, this is the model.
So it will use these fields to inform the columns and the data type.
Obviously string without any constraints would be like a var char mark.
So our var try to 55 whereas when we see a maxlength, it would set that particular lens on its right.
So that will go ahead and create the table.
Now let us look at our get cars method that we already had.
So I have in it, I think I should actually put the in it's inside of the track.
So try to initialize, right?
If we catch an exception we could throw or we could set something like message.
So I don't want to call a show from inside of service because that is right.
That is not to be messed with here as in car service, one for database operation.
So I just set us like a status message, string and status message.
And if we catch an exception, I don't want to throw because that would kill the application.
But we can set a message to say field to retrieve data.
So field to retrieve the if you want, you can add in the exception message.
But we already discussed that that's not the best thing to do necessarily right now.
After we initialize and we see, okay, the connection exists and so on, then what we can do is just
return a query so I can say contact table and then I give it the type that it should target and then
just return to list.
So what this will do is just go fetch a list of cars from the table via that connection and return it.
Otherwise it will just return unintelligible.
So if it fails to do this, it will.
Send back that our set that status message and then it will return an empty list.
All right.
So I think that's that's really good enough for now.
Now, the next thing that I want to do.
So we wrote code, but the app still doesn't know anything about the database.
Yes, we put in the library and we put in a bunch of things, but we need to actually configure the
database.
Right.
So the next thing that we want to do around here is and I'll just add that above everything else, I'll
say string DB path.
So remember DB Path, here is what I'm injecting into the service and then we need to go to the file
system that up there a trace.
So we'll just go file system, that app directory and we need to pass in what we want to call the file.
So actually I need to do a more like path combine.
So we have to formulate a path and then we go to the file system of directory and then we put in the
name of the file that we want.
So this would be cars dot DB three.
That's the extension, right?
Khazar db so we're just saying go to the file system directory and then create that skylight database
file for us.
And then the next thing I want to do is see builder dot services, dot add single ton.
So we're going to add a single ton instance of our service.
So we already have well, actually I already have that, but I already have the car services, so I'm
actually going to enhance this a bit where I'm going to see S inside of these braces as an activity
utilities dot create instance and then we'll just initialize the car service.
So create instance type bracket pass in the car service and then open and close braces again.
And then as I see s, which is the lambda token that we're using and then give it the DB path.
So that way when we initialize our car service, we know that we're passing in the DB path accordingly.
And I could actually just remove this part.
I could simplify it by removing that first start brackets, right?
So now that we have the car service configured, we have our MIUI DOT program does is configured.
Let's jump over to up that XAML, that CSS.
So I already wrote the code.
I'll just walk you through what I'm doing here.
So one, I'm setting up car service as a static, publicly accessible property of our app, right?
So that means I can just set up that get car service dot and call whatever I need to call it.
So we set it as a static public property and then in the constructor, of course, we do modify initialize
component, we allow it to do the main page and then we underneath that just initialize our car service.
So once we have that done, I can go back to my car list view model and I can remove the injection.
So remember that we had the property and then we had the injection or the field and then we injected
it and then we initialize it.
You can remove those lines.
All right.
So at the end of that activity, this is what your car list view model should look like.
And then the line that would give you an error is where we were retrieving the cars.
So now you can set up that car service dot get cars, right?
So those are the changes that you need to make after you have successfully configured the DB in the
output program.
That's CSS file.
All right.
Before we move forward with testing, there's one more thing I want to do.
And I erroneously did not set this as a public property.
Right.
So once it is public, we can actually see up that car service start and get the status message, which
is why we would use it here.
And then we could later on fire that message off as the up alert threat.
So let's just go ahead and test.
Let's run this.
All right.
So with our above, we are going to hit fetch cars.
But I set a breakpoint right here in the in it method so we can see exactly what happens.
Right?
So remember that it's going to get cars and it's going to call in its which should then create our database
and our table.
So when I fetched cars, we hit that breakpoint.
If I skip down to that line, if we look in the past, that is what that path looks like to the system
or to file system.
Of course, this would kind of look different based on your device.
And then if I step through, I would just step over and then it creates the card table and then it should
return the list of cards.
Of course, there is nothing in cards are known.
That count is zero.
But we do see that this will be working.
So when we come back, what we're going to do is modify the screen a bit so that we get a form where
we can fill out all of the fields and actually add cars.
So this screen is going to have the fields for the car, a button that says add car and it will retain
the car so that we can actually see the list come up underneath.
So stay tuned.