1 00:00:00,000 --> 00:00:02,790 Good, we were able to transform our 2 00:00:02,790 --> 00:00:05,280 script, which was built using functions. 3 00:00:05,820 --> 00:00:09,120 And we created a class and put those 4 00:00:09,120 --> 00:00:11,070 functions inside that class. So they are 5 00:00:11,070 --> 00:00:14,520 now called methods. And while this is a 6 00:00:14,520 --> 00:00:17,520 use of a class, it's still not the 7 00:00:17,520 --> 00:00:19,920 best use of Object Oriented Programming 8 00:00:19,920 --> 00:00:23,340 paradigm. Because we have a database 9 00:00:23,340 --> 00:00:25,650 here, a database object that is being 10 00:00:25,680 --> 00:00:29,010 created, but we are not treating it as a 11 00:00:29,010 --> 00:00:33,780 solid object. So we are opening it and 12 00:00:33,780 --> 00:00:37,590 closing it in every method. So what I'm 13 00:00:37,590 --> 00:00:41,820 thinking is, we create a connection when 14 00:00:41,820 --> 00:00:44,670 we call the class. So when this function 15 00:00:44,670 --> 00:00:47,310 is executed, we create a connection 16 00:00:47,310 --> 00:00:49,110 there, and we keep that connection 17 00:00:49,140 --> 00:00:53,850 opened. So I don't include this close 18 00:00:54,030 --> 00:00:56,790 method there. But I'll leave the commit 19 00:00:56,790 --> 00:01:01,020 method in case there is no table and a 20 00:01:01,020 --> 00:01:03,060 table will be created, and the changes 21 00:01:03,060 --> 00:01:04,980 will be committed to the open 22 00:01:04,980 --> 00:01:07,590 connection. Now, because we have a 23 00:01:07,590 --> 00:01:09,960 connection open there, we don't need to 24 00:01:09,960 --> 00:01:14,370 establish a new one in each method. We 25 00:01:14,370 --> 00:01:16,500 don't even need to create a cursor 26 00:01:16,500 --> 00:01:18,720 object, because the cursor object is 27 00:01:18,720 --> 00:01:21,420 being created in here. So we get rid of 28 00:01:21,450 --> 00:01:26,520 those two lines in every single method. 29 00:01:28,350 --> 00:01:31,410 So in view, we have a cursor object when 30 00:01:31,410 --> 00:01:34,380 we go here, and then we execute this 31 00:01:34,800 --> 00:01:36,990 statement to that cursor object. So that 32 00:01:36,990 --> 00:01:42,450 is good. There we go down here, these 33 00:01:42,450 --> 00:01:46,188 two also and for the delete method 34 00:01:46,189 --> 00:01:50,278 [Author typing] 35 00:01:50,280 --> 00:01:55,290 and update as well. Great. Now let's see how 36 00:01:55,290 --> 00:01:58,050 this goes. Let me make this closer. 37 00:01:58,052 --> 00:02:03,628 [Author typing] 38 00:02:03,630 --> 00:02:05,850 So we call the frontend script where we 39 00:02:05,850 --> 00:02:08,340 are creating an instance of our object. 40 00:02:09,090 --> 00:02:12,476 So far, so good View all. 41 00:02:12,477 --> 00:02:14,829 [No audio] 42 00:02:14,831 --> 00:02:21,054 And it says that name cur is not defined in line 17. 43 00:02:23,210 --> 00:02:28,680 So we executed the view method. And 44 00:02:28,680 --> 00:02:30,990 what the view method does is it tries to 45 00:02:30,990 --> 00:02:34,200 find a variable or declared variable 46 00:02:34,230 --> 00:02:36,930 inside the function. So the cur 47 00:02:36,930 --> 00:02:38,730 variable it doesn't find, so then it 48 00:02:38,730 --> 00:02:41,640 looks if there is a variable outside the 49 00:02:41,640 --> 00:02:44,160 function, and it doesn't find it. 50 00:02:44,160 --> 00:02:46,140 Actually, there is a variable that has 51 00:02:46,140 --> 00:02:49,380 been declared in this init function, but 52 00:02:49,410 --> 00:02:52,650 this is a local variable. So this has 53 00:02:52,650 --> 00:02:56,671 value only inside this function. So you cannot 54 00:02:56,672 --> 00:03:01,863 access this outside of the init function. But if we 55 00:03:01,864 --> 00:03:04,797 [Author typing] 56 00:03:04,798 --> 00:03:09,990 refer to the self argument, which is the database object 57 00:03:11,640 --> 00:03:16,230 and see what we get and we close this, save. 58 00:03:16,231 --> 00:03:20,368 [No audio] 59 00:03:20,370 --> 00:03:23,280 That works and View all. Now we get 60 00:03:23,310 --> 00:03:25,770 another type of error. And it says 61 00:03:25,770 --> 00:03:30,090 attribute error. So let me close this. So 62 00:03:30,090 --> 00:03:32,352 look carefully here, and it says 63 00:03:32,399 --> 00:03:35,460 a database object has no attribute cur. 64 00:03:36,090 --> 00:03:39,210 And that's in line 17. So this one 65 00:03:39,210 --> 00:03:42,570 here, the database object, this one is 66 00:03:42,570 --> 00:03:46,320 this one here. So self has been replaced 67 00:03:46,320 --> 00:03:50,250 by database, which is the object created 68 00:03:50,250 --> 00:03:54,600 by this class. This is an attribute of 69 00:03:54,630 --> 00:03:57,930 this object. To fix that, we need to 70 00:03:57,930 --> 00:04:01,710 create an attribute there. So we say 71 00:04:01,710 --> 00:04:05,730 self.cursor .cur equals to 72 00:04:05,730 --> 00:04:08,520 connection.cursor. So the cursor 73 00:04:08,520 --> 00:04:11,820 method or the connection object of the 74 00:04:11,820 --> 00:04:17,557 sqlite3 library. Now if we try again, the code 75 00:04:17,558 --> 00:04:20,149 [No audio] 76 00:04:20,151 --> 00:04:23,703 it says name cur is not defined in line 8. 77 00:04:25,432 --> 00:04:29,037 So now that's we created an attributes this is 78 00:04:29,054 --> 00:04:31,680 trying to find the variable cur. So the 79 00:04:31,680 --> 00:04:34,170 local variable cur, which is not in 80 00:04:34,170 --> 00:04:36,360 this function, because now this is an 81 00:04:36,360 --> 00:04:40,080 attribute of the class. So that means we 82 00:04:40,080 --> 00:04:43,080 need to refer to self here as well. So 83 00:04:43,080 --> 00:04:46,140 to the object and then cur.execute and 84 00:04:46,140 --> 00:04:50,910 so on. So, if we try this now, View all we 85 00:04:50,910 --> 00:04:52,920 get another line code is not defined in 86 00:04:52,920 --> 00:04:57,300 line 18. So here, that means you need to 87 00:04:57,300 --> 00:05:01,770 do the same and we don't have any 88 00:05:01,800 --> 00:05:04,410 cur variable anymore, we have the 89 00:05:04,410 --> 00:05:07,260 cur attribute. So now if I run this, 90 00:05:07,260 --> 00:05:09,750 again, I'll get probably an error about 91 00:05:09,750 --> 00:05:12,990 the connection this time, the conn 92 00:05:13,410 --> 00:05:16,890 variable, View all here, conn is not 93 00:05:16,890 --> 00:05:21,150 defined in line 19. So again, this view 94 00:05:21,150 --> 00:05:23,190 function is trying to find the variable 95 00:05:23,190 --> 00:05:27,090 conn connection. So self commit there, 96 00:05:27,660 --> 00:05:30,416 and here as well. So you declare the attribute 97 00:05:30,418 --> 00:05:34,852 there, and it can be the changes down here. 98 00:05:34,853 --> 00:05:38,283 [No audio] 99 00:05:38,285 --> 00:05:42,480 self.connection.close. So 100 00:05:42,480 --> 00:05:44,910 that looks good now. rows equals to 101 00:05:44,910 --> 00:05:47,370 this rows is just a local variable 102 00:05:47,580 --> 00:05:49,830 that is created here and then we return 103 00:05:49,860 --> 00:05:53,682 the fetched rows from this expression. 104 00:05:54,647 --> 00:05:57,269 So this method returns this when it is being 105 00:05:57,270 --> 00:06:03,694 called in this script, just here. Good. 106 00:06:03,695 --> 00:06:05,820 [No audio] 107 00:06:05,822 --> 00:06:09,757 And let me try what do we have at this point? 108 00:06:09,758 --> 00:06:12,786 [No audio] 109 00:06:12,788 --> 00:06:18,360 Line 7 name conn is not defined. Yeah, we 110 00:06:18,360 --> 00:06:22,740 need to do the same for these two. And 111 00:06:22,740 --> 00:06:25,710 everything looks good now. So let's try 112 00:06:25,740 --> 00:06:28,440 again. Yeah, View all and now we get the 113 00:06:28,440 --> 00:06:32,100 data there. But when I press it for a 114 00:06:32,100 --> 00:06:34,920 second time, we get yet another error. 115 00:06:35,940 --> 00:06:38,610 And so it says cannot operate on a close 116 00:06:38,640 --> 00:06:43,290 database. So why is that? Now well, I press 117 00:06:43,290 --> 00:06:46,050 the View button once, and this method was 118 00:06:46,050 --> 00:06:50,310 executed, so the cursor object was able 119 00:06:50,310 --> 00:06:54,510 to fetch these data. And then we close 120 00:06:54,510 --> 00:06:57,180 the connection or return the rows. So we 121 00:06:57,180 --> 00:07:00,780 were able to get those three rows there. 122 00:07:01,470 --> 00:07:03,210 But then when I press the View button 123 00:07:03,210 --> 00:07:06,660 for a second time, the view method faces 124 00:07:06,840 --> 00:07:09,750 close connection. So that's why you get 125 00:07:09,750 --> 00:07:11,910 this cannot operate on a close database 126 00:07:11,940 --> 00:07:15,934 error. So and see what we get. 127 00:07:17,674 --> 00:07:22,710 So View all it's working well. Yep. That means 128 00:07:22,710 --> 00:07:26,670 we can go ahead and modify the other 129 00:07:26,670 --> 00:07:30,970 methods. So self.cursor and 130 00:07:31,259 --> 00:07:35,730 self.connection and remove this close. Let's 131 00:07:35,730 --> 00:07:37,470 think about closing the connection later 132 00:07:37,470 --> 00:07:41,280 on. So don't focus on that now. Just try 133 00:07:41,280 --> 00:07:44,970 to make this method to work self here, 134 00:07:45,224 --> 00:07:51,014 self there, and there. Again, for the 135 00:07:51,060 --> 00:07:56,610 delete methods and remove the connection.close. 136 00:07:56,612 --> 00:08:02,368 [Author typing] 137 00:08:02,370 --> 00:08:04,920 And everything should work fine at this 138 00:08:04,920 --> 00:08:08,760 point. So let's see View all, well this is 139 00:08:08,760 --> 00:08:11,250 working, let's search for something to 140 00:08:11,250 --> 00:08:17,970 see and Search entry. We got one cursor there. 141 00:08:19,491 --> 00:08:25,281 So cur is now defined in search. Yeah. Sorry. 142 00:08:25,282 --> 00:08:27,400 [Author typing] 143 00:08:27,402 --> 00:08:29,652 Again, View all, View all, 144 00:08:29,653 --> 00:08:32,250 [Author typing] 145 00:08:32,250 --> 00:08:36,870 see Search entry. And yet another name 146 00:08:36,870 --> 00:08:39,750 error, name rows is not defined. 147 00:08:41,610 --> 00:08:46,680 So by mistake, I converted this local 148 00:08:46,680 --> 00:08:50,490 variable to an attributes. So rows here, rows 149 00:08:50,490 --> 00:08:56,340 there. Okay, one more time. View all, View all. 150 00:08:56,341 --> 00:08:58,978 [Author typing] 151 00:08:58,980 --> 00:09:00,585 And now seems to be working. 152 00:09:02,491 --> 00:09:04,050 So let's add some data. 153 00:09:04,051 --> 00:09:09,833 [Author typing] 154 00:09:09,835 --> 00:09:12,720 Add entry there and cannot operate on a 155 00:09:12,720 --> 00:09:19,440 close database. Why? Because all in 156 00:09:19,440 --> 00:09:21,780 the search method, we have a close 157 00:09:21,810 --> 00:09:24,851 database. So we close database there. And 158 00:09:24,852 --> 00:09:28,879 I overlooked that so we don't have any more. 159 00:09:28,880 --> 00:09:32,154 [No audio] 160 00:09:32,156 --> 00:09:35,402 close methods that are knew. So let's 161 00:09:35,403 --> 00:09:39,540 try one more time there. Sorry about that. 162 00:09:39,541 --> 00:09:46,168 [Author typing] 163 00:09:46,170 --> 00:09:48,649 Add entry and now it seems to be working fine. 164 00:09:48,650 --> 00:09:51,026 [Author typing] 165 00:09:51,028 --> 00:09:54,990 Great. So everything looks good. 166 00:09:55,380 --> 00:09:58,200 Except of we don't have the close 167 00:09:58,200 --> 00:10:00,840 method anymore because we can't keep 168 00:10:00,840 --> 00:10:03,300 that method, either method of our 169 00:10:03,300 --> 00:10:05,820 class. Because you saw that when we have 170 00:10:05,820 --> 00:10:08,490 that method there, and we end up with this 171 00:10:08,520 --> 00:10:11,910 error. Or you can leave the code without 172 00:10:12,780 --> 00:10:16,350 close method, but you'd done up with 173 00:10:16,350 --> 00:10:18,870 some problems later, because if you have 174 00:10:19,080 --> 00:10:20,550 some methods that have not been 175 00:10:20,550 --> 00:10:23,010 committed, then when another 176 00:10:23,010 --> 00:10:24,870 program connects to your database, and 177 00:10:24,870 --> 00:10:28,260 then these data will be committed with 178 00:10:28,260 --> 00:10:30,570 the next operation, so it messes some 179 00:10:30,570 --> 00:10:34,110 things up sometimes. So, as a rule, you 180 00:10:34,110 --> 00:10:37,140 should close the connection. And to do 181 00:10:37,140 --> 00:10:41,100 that very similar spatial methods that 182 00:10:41,100 --> 00:10:44,400 you can add to your class like this one 183 00:10:44,400 --> 00:10:47,730 here, but this other one is for 184 00:10:48,210 --> 00:10:52,140 destructing your object that is called 185 00:10:52,380 --> 00:10:57,090 del. So for delete, and this also gets 186 00:10:57,900 --> 00:11:00,630 the object thesis as a parameter. So 187 00:11:00,660 --> 00:11:05,070 this method is applied, is executed when 188 00:11:05,070 --> 00:11:08,040 you call them an instance of the class 189 00:11:08,040 --> 00:11:10,650 like we are doing in frontend script, 190 00:11:11,130 --> 00:11:15,090 just here. While this one here, this is 191 00:11:15,090 --> 00:11:18,690 executed when this instance is deleted 192 00:11:18,720 --> 00:11:21,750 from the script, which happens to be 193 00:11:21,750 --> 00:11:24,960 when the script is executed. So before 194 00:11:24,960 --> 00:11:29,390 the script exits this method will be executed. And 195 00:11:29,391 --> 00:11:33,808 what we want to do here is self.connection.close. 196 00:11:33,809 --> 00:11:35,872 [No audio] 197 00:11:35,874 --> 00:11:38,797 So that should do the trick. Now if you 198 00:11:38,798 --> 00:11:41,269 try out this, we should get the same 199 00:11:41,270 --> 00:11:43,743 [Author typing] 200 00:11:43,745 --> 00:11:47,500 results. So I added this by mistake. But 201 00:11:47,501 --> 00:11:51,570 anyway, so let's see, you can search for that and 202 00:11:51,570 --> 00:11:55,620 get the rows with that particular value. 203 00:11:57,420 --> 00:12:00,120 So this is my solution. And that's 204 00:12:00,120 --> 00:12:02,610 not standard way to write a script. So 205 00:12:02,850 --> 00:12:04,980 there may be other alternatives to 206 00:12:04,980 --> 00:12:08,040 solve this problem. So to build this 207 00:12:08,070 --> 00:12:11,040 application, so you may also choose to 208 00:12:11,670 --> 00:12:15,660 add the method there or here in the 209 00:12:16,110 --> 00:12:18,030 Graphical User Interface. So you could 210 00:12:18,030 --> 00:12:19,920 do something like you add a button like 211 00:12:19,950 --> 00:12:22,950 commit button, and then, here you add 212 00:12:22,950 --> 00:12:26,700 another method called commit. And then 213 00:12:26,700 --> 00:12:30,090 you remove probably all this commit 214 00:12:30,090 --> 00:12:32,850 methods from these methods from the 215 00:12:32,850 --> 00:12:36,180 methods of your SQLite operations, 216 00:12:36,210 --> 00:12:38,190 and then you put that method, so one 217 00:12:38,190 --> 00:12:42,720 method commit inside that commit method 218 00:12:42,750 --> 00:12:45,990 of your class. So when the user presses 219 00:12:45,990 --> 00:12:47,490 commit, and then you can make the changes 220 00:12:47,490 --> 00:12:49,320 to the database. So that would be 221 00:12:49,320 --> 00:12:52,380 another solution. But this is what we 222 00:12:52,380 --> 00:12:56,640 came up with in this particular case. So 223 00:12:56,640 --> 00:12:58,620 I hope you understand a few things. Now, 224 00:12:59,040 --> 00:13:01,530 Alice is rarely used. So normally, you 225 00:13:01,590 --> 00:13:03,630 only have to use init, and then the 226 00:13:03,630 --> 00:13:06,210 other methods, but this is appropriate 227 00:13:06,210 --> 00:13:09,090 when you want to apply something just 228 00:13:09,090 --> 00:13:11,700 before exiting the program. So that's 229 00:13:11,700 --> 00:13:14,640 great. And in the next lecture, how we will 230 00:13:14,670 --> 00:13:17,700 write another class. So we will think 231 00:13:17,700 --> 00:13:20,100 about how to model a problem, which is 232 00:13:20,100 --> 00:13:23,670 about creating some sort of bank account 233 00:13:23,700 --> 00:13:26,430 object where we store a balance of 234 00:13:26,430 --> 00:13:30,060 money. And then we withdraw and deposit 235 00:13:30,060 --> 00:13:32,940 the money into this balance. Now as an 236 00:13:32,940 --> 00:13:37,170 exercise, you can convert this script, 237 00:13:37,650 --> 00:13:40,800 solid tkinter part of the program of the 238 00:13:40,800 --> 00:13:44,220 frontend, into an Object Oriented 239 00:13:44,280 --> 00:13:48,180 form. So you put these inside a class 240 00:13:48,180 --> 00:13:50,760 and try to come up with a good 241 00:13:50,760 --> 00:13:53,430 solution. I'll also provide you a 242 00:13:53,430 --> 00:13:56,130 solution to this. But before that I would 243 00:13:56,130 --> 00:13:58,080 like to try out yourself. So you 244 00:13:58,080 --> 00:14:00,090 practice Object Oriented Programming a 245 00:14:00,090 --> 00:14:03,120 little bit. Yeah, that's good. We 246 00:14:03,120 --> 00:14:04,950 that's, I'll see you in the next lecture. 247 00:14:04,951 --> 00:14:06,995 [No audio]