Hey, in the previous video we created an empty database file and also a table inside that file that
was created by this class and also that method call there.
Now we want to be able to send this data to that database table.
So let's do that.
It's very easy to do.
And here is where you actually understand the benefit of having high level libraries such as SQL Alchemy.
So you just need to create a form instance.
So form is the class that we created up here, right?
Which contains the database model and form now gets as arguments the column names.
So first name is equal to first name.
So this is the argument name.
This is the value.
So as a value I'm giving this variable.
This variable holds the actual name of the user.
So if the user entered in the field in the first name field, then it will be stored in here as the
value of this argument.
So let's do the same for last name is equal to last name comma enter to split the line into multiple
lines.
It's more readable.
Email is equal to email, date is equal to date.
Occupation is equal to occupation.
Once you do that, you access the DB.
Variable which we created up here.
DB is equal to SQL alchemy.
And then you do session dot add form.
So you add the form to the session.
So the form now contains the values, the user values, and then you want to commit to that session.
So that will store the actual data to the database.
So we're performing basically like an insert SQL query here, but through a high level way.
With that, we can go to the web page, reload it, fill in some data.
One one 2023 self-employed submit and you might get this error for sure.
You will get it and let's read the error together.
So it says that SQLite data type only accepts Python data objects as inputs.
So you see now we now we start to see the actual commands which are executed behind the scenes.
So you see we have an insert into.
First name, last name, email, date, occupation, values these values.
And then we have the parameters.
So email this email.
Last name, occupation, first name date.
So the problem here is that we need to provide a python date object, right?
Not a string, because in here, when we get the date inside our index function, right, this date
will be a variable containing a string which will look something like this 2024 January 1st.
So this is what is stored in this date variable.
And that string is then sent to the database here.
But the database does not accept that since the database accepts a date type.
Right?
You see here in the form class, we specified a date type.
So let's fix this.
To fix that, you should work on the index function First, let me remove this here.
And so we need to get, let's say, a date object variable.
I'm storing the new date time object in this variable.
So you you can create date time objects in Python using the date time library.
So this is a standard library and you want to import the date time class out of that library.
And then you go down here inside the index function and you say date time dot strptime date.
So this variable which contains the string.
So the date is a string and you should declare what format this string has, right?
So the format is percentage capital Y minus percentage M.
I know this format when we print it out earlier using the debugger, we printed out the date as a string.
So it was in this format year minus month, minus percentage D for day.
So these are like format codes to represent dates and like years and months and days.
So with that we should be able to get an actual datetime object, which is like date time wise, so
to say.
And then we use that variable down here.
Right.
So when we instantiate the form now we want to say here date obj.
Right.
So then let's run the program again if it's not running already.
So run.
You can also rerun it.
No problem.
And reload here.
Fill in the data.
Let's say to ten for the month.
2020 for self-employed, perhaps submit.
Okay, so I submitted the data.
We see this view.
So the application form was emptied and now the data should have been stored in the data dot DB database.
And if you want to prove that, if you want, if you're curious to see the actual data, then you want
to use the DB browser for SQLite program.
And.
Yeah, open the database again.
You need to locate that file.
It's inside instance data dot DB.
Go to browse and yeah here are see some data already.
So that's the row I just added as a user.
So you see the name, the surname, the email address, the date and the occupation.
So there we got the data.
And that's the code.
So with that, we have implemented the part that stores the data in our database.
Now next is we want to display some message here that the data were sent were submitted successfully
or something like that, like a notification for the user so that the interface is user friendly because
currently the user presses the submit button, but there's nothing here telling them that the submission
actually worked.
So let's do that in the next video.
I'll see you there.