Good, we were able to transform our
script, which was built using functions.
And we created a class and put those
functions inside that class. So they are
now called methods. And while this is a
use of a class, it's still not the
best use of Object Oriented Programming
paradigm. Because we have a database
here, a database object that is being
created, but we are not treating it as a
solid object. So we are opening it and
closing it in every method. So what I'm
thinking is, we create a connection when
we call the class. So when this function
is executed, we create a connection
there, and we keep that connection
opened. So I don't include this close
method there. But I'll leave the commit
method in case there is no table and a
table will be created, and the changes
will be committed to the open
connection. Now, because we have a
connection open there, we don't need to
establish a new one in each method. We
don't even need to create a cursor
object, because the cursor object is
being created in here. So we get rid of
those two lines in every single method.
So in view, we have a cursor object when
we go here, and then we execute this
statement to that cursor object. So that
is good. There we go down here, these
two also and for the delete method
[Author typing]
and update as well. Great. Now let's see how
this goes. Let me make this closer.
[Author typing]
So we call the frontend script where we
are creating an instance of our object.
So far, so good View all.
[No audio]
And it says that name cur is not defined in line 17.
So we executed the view method. And
what the view method does is it tries to
find a variable or declared variable
inside the function. So the cur
variable it doesn't find, so then it
looks if there is a variable outside the
function, and it doesn't find it.
Actually, there is a variable that has
been declared in this init function, but
this is a local variable. So this has
value only inside this function. So you cannot
access this outside of the init function. But if we
[Author typing]
refer to the self argument, which is the database object
and see what we get and we close this, save.
[No audio]
That works and View all. Now we get
another type of error. And it says
attribute error. So let me close this. So
look carefully here, and it says
a database object has no attribute cur.
And that's in line 17. So this one
here, the database object, this one is
this one here. So self has been replaced
by database, which is the object created
by this class. This is an attribute of
this object. To fix that, we need to
create an attribute there. So we say
self.cursor .cur equals to
connection.cursor. So the cursor
method or the connection object of the
sqlite3 library. Now if we try again, the code
[No audio]
it says name cur is not defined in line 8.
So now that's we created an attributes this is
trying to find the variable cur. So the
local variable cur, which is not in
this function, because now this is an
attribute of the class. So that means we
need to refer to self here as well. So
to the object and then cur.execute and
so on. So, if we try this now, View all we
get another line code is not defined in
line 18. So here, that means you need to
do the same and we don't have any
cur variable anymore, we have the
cur attribute. So now if I run this,
again, I'll get probably an error about
the connection this time, the conn
variable, View all here, conn is not
defined in line 19. So again, this view
function is trying to find the variable
conn connection. So self commit there,
and here as well. So you declare the attribute
there, and it can be the changes down here.
[No audio]
self.connection.close. So
that looks good now. rows equals to
this rows is just a local variable
that is created here and then we return
the fetched rows from this expression.
So this method returns this when it is being
called in this script, just here. Good.
[No audio]
And let me try what do we have at this point?
[No audio]
Line 7 name conn is not defined. Yeah, we
need to do the same for these two. And
everything looks good now. So let's try
again. Yeah, View all and now we get the
data there. But when I press it for a
second time, we get yet another error.
And so it says cannot operate on a close
database. So why is that? Now well, I press
the View button once, and this method was
executed, so the cursor object was able
to fetch these data. And then we close
the connection or return the rows. So we
were able to get those three rows there.
But then when I press the View button
for a second time, the view method faces
close connection. So that's why you get
this cannot operate on a close database
error. So and see what we get.
So View all it's working well. Yep. That means
we can go ahead and modify the other
methods. So self.cursor and
self.connection and remove this close. Let's
think about closing the connection later
on. So don't focus on that now. Just try
to make this method to work self here,
self there, and there. Again, for the
delete methods and remove the connection.close.
[Author typing]
And everything should work fine at this
point. So let's see View all, well this is
working, let's search for something to
see and Search entry. We got one cursor there.
So cur is now defined in search. Yeah. Sorry.
[Author typing]
Again, View all, View all,
[Author typing]
see Search entry. And yet another name
error, name rows is not defined.
So by mistake, I converted this local
variable to an attributes. So rows here, rows
there. Okay, one more time. View all, View all.
[Author typing]
And now seems to be working.
So let's add some data.
[Author typing]
Add entry there and cannot operate on a
close database. Why? Because all in
the search method, we have a close
database. So we close database there. And
I overlooked that so we don't have any more.
[No audio]
close methods that are knew. So let's
try one more time there. Sorry about that.
[Author typing]
Add entry and now it seems to be working fine.
[Author typing]
Great. So everything looks good.
Except of we don't have the close
method anymore because we can't keep
that method, either method of our
class. Because you saw that when we have
that method there, and we end up with this
error. Or you can leave the code without
close method, but you'd done up with
some problems later, because if you have
some methods that have not been
committed, then when another
program connects to your database, and
then these data will be committed with
the next operation, so it messes some
things up sometimes. So, as a rule, you
should close the connection. And to do
that very similar spatial methods that
you can add to your class like this one
here, but this other one is for
destructing your object that is called
del. So for delete, and this also gets
the object thesis as a parameter. So
this method is applied, is executed when
you call them an instance of the class
like we are doing in frontend script,
just here. While this one here, this is
executed when this instance is deleted
from the script, which happens to be
when the script is executed. So before
the script exits this method will be executed. And
what we want to do here is self.connection.close.
[No audio]
So that should do the trick. Now if you
try out this, we should get the same
[Author typing]
results. So I added this by mistake. But
anyway, so let's see, you can search for that and
get the rows with that particular value.
So this is my solution. And that's
not standard way to write a script. So
there may be other alternatives to
solve this problem. So to build this
application, so you may also choose to
add the method there or here in the
Graphical User Interface. So you could
do something like you add a button like
commit button, and then, here you add
another method called commit. And then
you remove probably all this commit
methods from these methods from the
methods of your SQLite operations,
and then you put that method, so one
method commit inside that commit method
of your class. So when the user presses
commit, and then you can make the changes
to the database. So that would be
another solution. But this is what we
came up with in this particular case. So
I hope you understand a few things. Now,
Alice is rarely used. So normally, you
only have to use init, and then the
other methods, but this is appropriate
when you want to apply something just
before exiting the program. So that's
great. And in the next lecture, how we will
write another class. So we will think
about how to model a problem, which is
about creating some sort of bank account
object where we store a balance of
money. And then we withdraw and deposit
the money into this balance. Now as an
exercise, you can convert this script,
solid tkinter part of the program of the
frontend, into an Object Oriented
form. So you put these inside a class
and try to come up with a good
solution. I'll also provide you a
solution to this. But before that I would
like to try out yourself. So you
practice Object Oriented Programming a
little bit. Yeah, that's good. We
that's, I'll see you in the next lecture.
[No audio]