1
00:00:00,820 --> 00:00:07,210
Hey, in the previous video we created an empty database file and also a table inside that file that

2
00:00:07,210 --> 00:00:13,460
was created by this class and also that method call there.

3
00:00:13,480 --> 00:00:18,180
Now we want to be able to send this data to that database table.

4
00:00:18,190 --> 00:00:19,390
So let's do that.

5
00:00:21,230 --> 00:00:22,490
It's very easy to do.

6
00:00:22,490 --> 00:00:30,380
And here is where you actually understand the benefit of having high level libraries such as SQL Alchemy.

7
00:00:30,560 --> 00:00:35,150
So you just need to create a form instance.

8
00:00:35,990 --> 00:00:40,760
So form is the class that we created up here, right?

9
00:00:40,760 --> 00:00:48,380
Which contains the database model and form now gets as arguments the column names.

10
00:00:48,380 --> 00:00:52,040
So first name is equal to first name.

11
00:00:52,040 --> 00:00:54,350
So this is the argument name.

12
00:00:54,350 --> 00:00:55,990
This is the value.

13
00:00:56,000 --> 00:00:59,780
So as a value I'm giving this variable.

14
00:00:59,780 --> 00:01:03,740
This variable holds the actual name of the user.

15
00:01:03,740 --> 00:01:12,770
So if the user entered in the field in the first name field, then it will be stored in here as the

16
00:01:12,770 --> 00:01:14,330
value of this argument.

17
00:01:14,330 --> 00:01:23,220
So let's do the same for last name is equal to last name comma enter to split the line into multiple

18
00:01:23,220 --> 00:01:23,820
lines.

19
00:01:23,820 --> 00:01:25,080
It's more readable.

20
00:01:25,350 --> 00:01:29,820
Email is equal to email, date is equal to date.

21
00:01:30,960 --> 00:01:35,520
Occupation is equal to occupation.

22
00:01:35,910 --> 00:01:38,400
Once you do that, you access the DB.

23
00:01:39,900 --> 00:01:42,050
Variable which we created up here.

24
00:01:42,080 --> 00:01:44,300
DB is equal to SQL alchemy.

25
00:01:44,300 --> 00:01:49,520
And then you do session dot add form.

26
00:01:49,640 --> 00:01:51,830
So you add the form to the session.

27
00:01:51,830 --> 00:02:00,800
So the form now contains the values, the user values, and then you want to commit to that session.

28
00:02:00,800 --> 00:02:04,370
So that will store the actual data to the database.

29
00:02:04,370 --> 00:02:11,390
So we're performing basically like an insert SQL query here, but through a high level way.

30
00:02:11,630 --> 00:02:17,330
With that, we can go to the web page, reload it, fill in some data.

31
00:02:18,920 --> 00:02:27,560
One one 2023 self-employed submit and you might get this error for sure.

32
00:02:27,560 --> 00:02:30,560
You will get it and let's read the error together.

33
00:02:30,560 --> 00:02:37,640
So it says that SQLite data type only accepts Python data objects as inputs.

34
00:02:37,820 --> 00:02:46,040
So you see now we now we start to see the actual commands which are executed behind the scenes.

35
00:02:46,040 --> 00:02:49,040
So you see we have an insert into.

36
00:02:49,680 --> 00:02:53,730
First name, last name, email, date, occupation, values these values.

37
00:02:53,850 --> 00:02:56,040
And then we have the parameters.

38
00:02:56,040 --> 00:02:57,690
So email this email.

39
00:02:57,690 --> 00:03:01,260
Last name, occupation, first name date.

40
00:03:01,770 --> 00:03:07,800
So the problem here is that we need to provide a python date object, right?

41
00:03:07,830 --> 00:03:17,070
Not a string, because in here, when we get the date inside our index function, right, this date

42
00:03:17,100 --> 00:03:25,590
will be a variable containing a string which will look something like this 2024 January 1st.

43
00:03:25,620 --> 00:03:30,030
So this is what is stored in this date variable.

44
00:03:30,030 --> 00:03:35,070
And that string is then sent to the database here.

45
00:03:35,070 --> 00:03:42,060
But the database does not accept that since the database accepts a date type.

46
00:03:42,060 --> 00:03:42,750
Right?

47
00:03:42,780 --> 00:03:46,770
You see here in the form class, we specified a date type.

48
00:03:46,770 --> 00:03:48,810
So let's fix this.

49
00:03:48,810 --> 00:03:56,110
To fix that, you should work on the index function First, let me remove this here.

50
00:03:56,590 --> 00:04:02,200
And so we need to get, let's say, a date object variable.

51
00:04:02,200 --> 00:04:07,240
I'm storing the new date time object in this variable.

52
00:04:07,240 --> 00:04:14,620
So you you can create date time objects in Python using the date time library.

53
00:04:14,620 --> 00:04:21,670
So this is a standard library and you want to import the date time class out of that library.

54
00:04:22,660 --> 00:04:32,860
And then you go down here inside the index function and you say date time dot strptime date.

55
00:04:32,860 --> 00:04:35,440
So this variable which contains the string.

56
00:04:35,470 --> 00:04:42,130
So the date is a string and you should declare what format this string has, right?

57
00:04:42,130 --> 00:04:47,770
So the format is percentage capital Y minus percentage M.

58
00:04:47,770 --> 00:04:54,570
I know this format when we print it out earlier using the debugger, we printed out the date as a string.

59
00:04:54,570 --> 00:05:02,910
So it was in this format year minus month, minus percentage D for day.

60
00:05:03,060 --> 00:05:10,890
So these are like format codes to represent dates and like years and months and days.

61
00:05:11,160 --> 00:05:18,990
So with that we should be able to get an actual datetime object, which is like date time wise, so

62
00:05:18,990 --> 00:05:19,740
to say.

63
00:05:19,740 --> 00:05:24,360
And then we use that variable down here.

64
00:05:25,220 --> 00:05:25,490
Right.

65
00:05:25,490 --> 00:05:32,990
So when we instantiate the form now we want to say here date obj.

66
00:05:33,350 --> 00:05:34,100
Right.

67
00:05:34,280 --> 00:05:37,730
So then let's run the program again if it's not running already.

68
00:05:37,760 --> 00:05:38,870
So run.

69
00:05:39,080 --> 00:05:40,880
You can also rerun it.

70
00:05:40,910 --> 00:05:41,840
No problem.

71
00:05:42,110 --> 00:05:43,850
And reload here.

72
00:05:44,450 --> 00:05:45,920
Fill in the data.

73
00:05:45,950 --> 00:05:48,860
Let's say to ten for the month.

74
00:05:48,890 --> 00:05:53,900
2020 for self-employed, perhaps submit.

75
00:05:54,320 --> 00:05:56,890
Okay, so I submitted the data.

76
00:05:56,900 --> 00:05:59,180
We see this view.

77
00:05:59,180 --> 00:06:07,730
So the application form was emptied and now the data should have been stored in the data dot DB database.

78
00:06:07,970 --> 00:06:14,900
And if you want to prove that, if you want, if you're curious to see the actual data, then you want

79
00:06:14,900 --> 00:06:18,890
to use the DB browser for SQLite program.

80
00:06:19,910 --> 00:06:20,630
And.

81
00:06:20,630 --> 00:06:23,600
Yeah, open the database again.

82
00:06:23,600 --> 00:06:25,520
You need to locate that file.

83
00:06:27,300 --> 00:06:29,550
It's inside instance data dot DB.

84
00:06:31,050 --> 00:06:37,890
Go to browse and yeah here are see some data already.

85
00:06:37,890 --> 00:06:41,610
So that's the row I just added as a user.

86
00:06:41,610 --> 00:06:48,270
So you see the name, the surname, the email address, the date and the occupation.

87
00:06:48,970 --> 00:06:50,740
So there we got the data.

88
00:06:52,080 --> 00:06:53,250
And that's the code.

89
00:06:53,810 --> 00:06:59,930
So with that, we have implemented the part that stores the data in our database.

90
00:06:59,960 --> 00:07:08,390
Now next is we want to display some message here that the data were sent were submitted successfully

91
00:07:08,390 --> 00:07:16,190
or something like that, like a notification for the user so that the interface is user friendly because

92
00:07:16,190 --> 00:07:22,130
currently the user presses the submit button, but there's nothing here telling them that the submission

93
00:07:22,130 --> 00:07:23,330
actually worked.

94
00:07:23,330 --> 00:07:25,010
So let's do that in the next video.

95
00:07:25,010 --> 00:07:25,550
I'll see you there.

