Hey, welcome back.
In the previous video, we accessed the data entered by the user using this code here.
So now we have those data stored in variables.
And the next step is to get the values from these variables and store them in an SQL database.
So we're going to use SQLite but through the SQL Alchemy Library.
So you need to import from FLASK SQL Alchemy, which is the library which you need to install.
So Flask underscore SQL Alchemy, you need to open your terminal and do PIP install flask, but use
a dash so you import it with an underscore in between these two words.
But then to install it you should use a dash.
So Flask SQL Alchemy.
So this is a separate line vary, but it does interact with flask.
So from flask SQL alchemy you want to import.
The SQL.
Alchemy class.
So why are we using SQL Alchemy and not using the SQLite three library which we use previously in the
course in the apps, in the desktop apps?
Well, because this library allows us to interact with the database using more high level codes, which
makes it easier for us to manage the database and the data.
You'll see how exactly.
So once we import this, then below the app instance, you want to create a database instance with DB
is equal to SQL alchemy and you provide the app variable which holds the flask instance as the argument
of this class.
So that will create an SQL Alchemy database instance.
Now, in between these two we should specify a series of parameters using App.config, which is a dictionary.
And out of this dictionary we should access.
The keys of this dictionary.
So App.config is a dictionary?
Secret.
Secret key is one of the keys of that dictionary.
And we want to place to set here a secret key for our app.
This will guard the application from hackers for things like cookies, tampering and hijacking the session.
So those hackers will need this key here if they want to perform those actions.
So just set a key.
Whatever you like.
Like my application.
One, two, three.
Just something simple for now.
So that's the first parameter.
The second parameter is app dot config.
Again, you use the same syntax.
So a dictionary and this will be SQL alchemy underscore database.
Underscore Uri.
Uri, I'm going to provide these also as lecture resources so you can just copy them and paste them
because this is quite fragile.
It's easy to to do a typo here and things will not work and it will be difficult to troubleshoot it.
So this will be equal to SQLite colon slash, slash, slash three slashes and then data dot DB.
So what is this?
Well, this specifies that the URI of the database is this one here.
So the URI specifies that we're using an SQLite database.
If we were using MySQL, this would be something like my SQL and so on.
But in this case it's SQLite and this is the name we want to have for the database file, which will
be created by flask.
So that's the value of this key of that dictionary.
Uri.
So these two specify the parameters of the database.
Now, the next step is to create the database itself.
To create the database, we need a database module and we create a database module using a class.
Let's name this class form.
And this should inherit from DB dot model this year.
So DB is an instance of an SQL alchemy class.
So this class has this module property which is also a class, right?
And so this form will inherit from this module class.
And here we simply specify the fields of the database table.
So let's start with an ID.
This is going to be DB dot column, DB dot integer.
So the DB instance does have a lot of attributes such as column integer.
Let's set this as a primary key.
So primary key, set it to true.
So this will be the first field of the database table, the first column.
This is usually the standard practice.
You create an ID and you set it as the primary key.
So this will be like a unique ID that identifies each row of the data of the table.
So in the table we're going to have an ID for each user submission, right?
So user IDs will have one row in the database table.
And so here we'll have the first name.
Of that user, which is again DB column and the ID should be DB dot string with capital S.
So don't confuse this with the string data type of python.
This is from DB, not from the python standard library.
String type.
So let's set an 80 characters maximum for the first name.
I'm going to duplicate this for the last name as well.
I'm going to duplicate it for email.
Let's put it as a string for the email as well.
SQLite doesn't have a specific type for emails and then we've got available date or date.
So this is a date that the candidates, the job candidate wants to start the new job.
Now this is going to be db.date and remove that.
Also the parentheses so db.date not with parentheses.
Another duplication and this will be occupation.
This will also be a string.
So we'll store here unemployed or employed or self-employed or students that string.
Let's set it at.
80 characters maximum.
So that's our database model.
Now we need to execute this class somehow so that we create the table in this file, which is also going
to be created because it doesn't exist yet here in our project directory.
To create the database from this model.
We should go here to the very end.
And first, let's add this routine here.
If name is equal to Main, I've explained this several times what this means, so you need to intend
that there.
You also need to add something else with app dot app context with parentheses and you indent that to
one more time.
And before that you say DB dot create all.
So what this will do is it will create a database because DB points to this variable, right?
So let's try this.
Let's run the app.
So this is app.py.
I'm going to run it.
Reload the page.
Go here.
And if you expand the project directory now, you'll see that an instance directory has been automatically
created and inside that instance you should see the data dot DB file which corresponds to this file
name here.
Data dot db.
So when we run the Flask app, this database was created.
If you run the Flask app for a second time, the database will not be recreated because this method
basically checks if there exists a database with that name, with that URI, and if it exists, it will
not overwrite it, but it will use the existing database.
Now, what is inside this database, you might ask?
Well, to check that you need a third party program DB browser for SQLite.
You can search for that program online and install it.
It's easy to install that program.
And so then you open it and you should see something like this, and then you could go to open database
and locate that particular FLASK app.
So this is my project app 16 flask form.
If I go to instance, I locate the data dot DB file, I open it and then you want to go to browse and
you want to select the form table here.
So you see it has created a table named form with an ID, first name, last name, email, date and
occupation fields.
So that's where we are so far.
Using the SQL Alchemy class, we were able to get this class, this database model, and create a database
out of that.
So this class is connected to SQL Alchemy using this DB dot model.
And so then you say DB, DB dot create all and that class will be called and those columns will be added
to the database.
So this is quite a high level usage of interacting with databases.
So with this, this lecture is complete and next we are ready to store the user data to the database
that we created in this video.
So let's do that in the next one.