1
00:00:00,030 --> 00:00:05,160
Good, we were able to transform our
script which was built using functions,

2
00:00:05,160 --> 00:00:11,219
and we created a class and put those
functions inside that class, so they are now

3
00:00:11,219 --> 00:00:18,199
called methods. And while this is a use
of a class, it's still not the best use

4
00:00:18,199 --> 00:00:23,820
of object-oriented programming paradigm
because we have a database here,

5
00:00:23,820 --> 00:00:29,400
a database object that is being created
but we are not treating it as a solid

6
00:00:29,400 --> 00:00:38,010
object, so we are opening it and closing
it in every method, so what I'm thinking

7
00:00:38,010 --> 00:00:44,760
is we create a connection when we call
the class, so when this function is

8
00:00:44,760 --> 00:00:50,870
executed we create a connection there,
and we keep the connection opened, so I

9
00:00:50,870 --> 00:00:57,270
don't include this close method there,
but I'll leave the commit method in

10
00:00:57,270 --> 00:01:03,210
case there is no table, and a table will
be created and the changes will be

11
00:01:03,210 --> 00:01:08,820
committed to the open connection.
Now because we have a connection open there

12
00:01:08,820 --> 00:01:15,570
we don't need to establish a new one in
each method and we don't even need to

13
00:01:15,570 --> 00:01:19,590
create a cursor object because the
cursor object is being created in here.

14
00:01:19,590 --> 00:01:29,640
So we get rid of those two lines in
every single method. So in view we have a

15
00:01:29,640 --> 00:01:35,939
cursor object when we go here and then
we execute this statement to that cursor

16
00:01:35,939 --> 00:01:45,810
object, so that is good. Then we go down
here, this to as well,

17
00:01:45,810 --> 00:01:48,170
and for the delete method.
And update as well. Great.

18
00:01:54,980 --> 00:02:04,640
Now let's see how this goes.
Let me make this closer, so we call the

19
00:02:04,640 --> 00:02:11,050
frontend script where we are creating an
instance of our object, so far so good.

20
00:02:11,050 --> 00:02:22,420
View all and it says that name cur
is not defined in line 17.

21
00:02:22,420 --> 00:02:31,010
So we executed the view method, and what
the view method does is it tries to find

22
00:02:31,010 --> 00:02:37,459
a variable, a declared variable inside
the function, so the cur variable.

23
00:02:37,459 --> 00:02:43,100
It doesn't find so then it looks if there
is a variable outside the function and

24
00:02:43,100 --> 00:02:47,480
it doesn't find it. Actually there is a
variable that has been declared in this

25
00:02:47,480 --> 00:02:54,260
init function, but this is a local
variable, so this has value only inside

26
00:02:54,260 --> 00:03:03,730
this function, so you cannot access this
outside of the init function. But if we

27
00:03:04,299 --> 00:03:12,530
refer to the self argument which is the
database object, and see what we get.

28
00:03:12,530 --> 00:03:25,250
Let me close this. Save, and that works and
view all, and now we get another type of error.

29
00:03:25,250 --> 00:03:32,060
And it says attribute error, so let me close
this, so look carefully here and it says

30
00:03:32,060 --> 00:03:39,410
database object has no attribute cur.
And that's in line 17, so this one here.

31
00:03:39,410 --> 00:03:46,549
And the database object, this one is this
on here, so self has very placed by

32
00:03:46,549 --> 00:03:54,350
database which is the object created by
this class and this is an attribute of

33
00:03:54,350 --> 00:04:02,060
this object and to fix that we need to
create an attribute there.

34
00:04:02,060 --> 00:04:08,090
So we say self dot cursor, dot cur
equals to connection dot cursor.

35
00:04:08,090 --> 00:04:15,799
the cursor method of the connection
object of the sqlite3, a library.

36
00:04:15,799 --> 00:04:26,509
Now if we try again the code, it says name
cur is not defined in line 8. So now that

37
00:04:26,509 --> 00:04:31,699
we created an attribute, this is
trying to find the variable cur, so the

38
00:04:31,699 --> 00:04:36,350
local variable cur which is not in
this function because now this is an

39
00:04:36,350 --> 00:04:43,130
attribute of the class so that means we
need to refer to self here as well, so to

40
00:04:43,130 --> 00:04:51,080
the object and then cur execute and so on.
So if we try this now view all, we get

41
00:04:51,080 --> 00:04:57,680
another line. Cur is not defined in line 18.
So here, that means you need to do the

42
00:04:57,680 --> 00:05:04,880
same and we don't have any curve
variable anymore. We have the cur attribute.

43
00:05:04,880 --> 00:05:10,400
So now if I run this again I'll get
probably an error about the connection

44
00:05:10,400 --> 00:05:20,720
this time. The conn variable. View all.
Conn is not defined in line 19, so again

45
00:05:20,720 --> 00:05:25,820
this view function is trying to find the
variable conn, connection, so self

46
00:05:25,820 --> 00:05:33,260
commit there and here as well, so you
declare the attribute there, and then you commit

47
00:05:33,260 --> 00:05:44,720
the changes down here, and self connection,
close, so that looks good now. Rows is equal

48
00:05:44,720 --> 00:05:49,130
to this, so rows is just a local variable.
That is created here and then we

49
00:05:49,130 --> 00:05:55,639
returned the fetched rows from
this expression, so this method returns

50
00:05:55,639 --> 00:06:05,490
this when it's being called in this
script, just here, good.

51
00:06:05,490 --> 00:06:15,270
And let me try what we have at this point.
Line seven, name conn is not defined.

52
00:06:15,270 --> 00:06:23,670
Yeah, we need to do the same
for these two, and everything looks good

53
00:06:23,670 --> 00:06:30,840
now, so let's try again. Yeah, view all and
now we get the data there, but when I

54
00:06:30,840 --> 00:06:37,230
press it for the second time we get yet
another error, and so it says cannot

55
00:06:37,230 --> 00:06:44,670
operate on a closed database, so why is that?
Well I pressed the view button once and

56
00:06:44,670 --> 00:06:52,460
this method was executed, so the cursor
object was able to fetch these data, and

57
00:06:52,460 --> 00:06:58,380
then we closed the connection, I returned
the rows, so we are able to get those

58
00:06:58,380 --> 00:07:04,950
three rows there, but then when I pressed
the view button for a second time,

59
00:07:04,950 --> 00:07:10,590
the view method faces a close connection.
So that's why you get this cannot operate

60
00:07:10,590 --> 00:07:16,970
on a closed database error.
So, see what we get.

61
00:07:16,970 --> 00:07:26,160
So view all. It's working well, yep.
That means we can go ahead and modify the

62
00:07:26,160 --> 00:07:35,460
other methods, so self cursor and self
connection, and remove this close.

63
00:07:35,460 --> 00:07:39,800
Let's think about closing the connection
later on, so don't focus on that now.

64
00:07:39,800 --> 00:07:50,130
Just try to make these methods work.
Self here, self there, and there, again for

65
00:07:50,130 --> 00:07:58,250
the delete method, and yeah remove
the connection, close.

66
00:08:00,310 --> 00:08:08,750
And everything should work fine at this
point so let's see. View all. This is

67
00:08:08,750 --> 00:08:17,240
working, let's search for something. "The Sea"
and search entry. We got one cursor there.

68
00:08:17,240 --> 00:08:27,979
So cur is not defined in search.
Oh yeah, sorry! Again. View all.

69
00:08:27,979 --> 00:08:38,839
View all. "The Sea" search entry and yet
another name error.

70
00:08:38,839 --> 00:08:46,339
The name rows is not defined.
So by mistake I converted this

71
00:08:46,339 --> 00:08:55,339
local variable to attributes, so rows
here, rows there. Okay one more time.

72
00:08:55,339 --> 00:09:10,640
View all. View all. Now it seems to be working.
So let's add some data. Add entry there.

73
00:09:10,640 --> 00:09:17,890
And cannot operate on closed database.
Why?

74
00:09:17,890 --> 00:09:23,149
Because all in the search method we have
a closed database, so we closed the

75
00:09:23,149 --> 00:09:33,020
database there, and I overlooked that,
so we don't have any more closed methods

76
00:09:33,020 --> 00:09:41,140
there, no. So let's try one more time then.
Sorry about that!

77
00:09:45,670 --> 00:09:54,820
Add entry and now it seems to be working
fine, great, so everything looks good

78
00:09:54,820 --> 00:10:00,770
except of we don't have the close
method anymore because we can't keep

79
00:10:00,770 --> 00:10:05,959
that method in the methods of our class.
Because you saw that when we have that

80
00:10:05,959 --> 00:10:09,270
method there and we end up
with this error.

81
00:10:09,270 --> 00:10:16,560
Or you can leave the code without
a close method, but you'd end up with some

82
00:10:16,560 --> 00:10:21,080
problems later because if you have some
methods that have not been committed,

83
00:10:21,080 --> 00:10:26,310
then one when another program connects
to your database, then these data

84
00:10:26,310 --> 00:10:30,810
will be committed with the next
operation, so it messes something up

85
00:10:30,810 --> 00:10:37,980
sometimes, so as a rule you should close
the connection, and to do that there is

86
00:10:37,980 --> 00:10:45,080
another special method that you can add
to your class like this one here, but

87
00:10:45,080 --> 00:10:54,510
this other one is for destructing your
object. That is called del, so for delete.

88
00:10:54,510 --> 00:11:02,839
And this also gets the object instance
as a parameter, so this method is applied,

89
00:11:02,839 --> 00:11:09,510
it's executed when you call an instance
of the class like we are doing in the

90
00:11:09,510 --> 00:11:16,940
frontend script, just here, while this
one here it is executed when this

91
00:11:16,940 --> 00:11:22,860
instance is deleted from the script
which happens to be when the script is

92
00:11:22,860 --> 00:11:30,029
exited, so before the script exits this
method will be executed. And what we want

93
00:11:30,029 --> 00:11:38,640
to do here is self dot connection dot close.
So that should do the trick. Now if

94
00:11:38,640 --> 00:11:43,100
we try out this, we should get the same
results, so I added this by mistake but

95
00:11:47,520 --> 00:11:54,570
anyway, so "The Sea". You can search for that.
And you get the rows with that particular

96
00:11:54,570 --> 00:12:01,680
value, so this is my solution
and there's no standard way to write

97
00:12:01,680 --> 00:12:06,959
a script so there may be other
alternatives to solve this problem,

98
00:12:06,959 --> 00:12:13,850
so to build this application so you may
also choose to add a method there or

99
00:12:13,850 --> 00:12:19,140
here in the graphical user interface so
you could do something like you add

100
00:12:19,140 --> 00:12:23,279
a button like commit button,
and then here you add another

101
00:12:23,279 --> 00:12:31,170
method called commit, and then you remove
probably all these commit methods from these

102
00:12:31,170 --> 00:12:37,800
methods, from the methods of your SQLite
operations, and then you put that method,

103
00:12:37,800 --> 00:12:45,509
so one method commit inside that commit
method of your class. So when the user

104
00:12:45,509 --> 00:12:49,319
presses commit, then you commit the changes
to the database, so that would be

105
00:12:49,319 --> 00:12:56,610
another solution, but this is what we
came up with in this particular case, so

106
00:12:56,610 --> 00:13:01,290
I hope you understand a few things now.
This is rarely used, so normally you

107
00:13:01,290 --> 00:13:05,910
only have to use init and then the
other methods, but this is appropriate

108
00:13:05,910 --> 00:13:11,639
when you want to apply something just
before exiting the program, so that's

109
00:13:11,639 --> 00:13:17,699
great and in the next lecture I will
write another class, so we will think

110
00:13:17,699 --> 00:13:23,670
about how to model a problem which is
about creating some sort of bank account

111
00:13:23,670 --> 00:13:30,180
object where we store a balance of money,
Then we withdraw and deposited money

112
00:13:30,180 --> 00:13:38,189
to this balance. Now as an exercise
you can convert this script, so the tkinter

113
00:13:38,189 --> 00:13:46,860
part of the program, the frontend
into an object-oriented form so you put

114
00:13:46,860 --> 00:13:53,160
these inside a class and try to come up
with a good solution. I'll also provide

115
00:13:53,160 --> 00:13:58,079
you a solution to this, but before that I
would like you to try out yourself, so you

116
00:13:58,079 --> 00:14:03,329
practice object-oriented programming
a little bit. Yeah, that's good. With that

117
00:14:03,329 --> 00:14:04,860
I'll see you in the next lecture.

