1 00:00:00,340 --> 00:00:04,580 Our models have five really useful methods that we'll be using a lot. 2 00:00:04,580 --> 00:00:06,780 Create, adds a new record to the table. 3 00:00:06,780 --> 00:00:10,010 Select, let's us pick rows out of the table to use. 4 00:00:10,010 --> 00:00:13,110 Save, will update an existing row in the database. 5 00:00:13,110 --> 00:00:15,580 Get, will fetch a single record from the database. 6 00:00:15,580 --> 00:00:19,200 And finally, delete_instance will delete a row from a table. 7 00:00:19,200 --> 00:00:22,180 There are many other methods on Peewee models but I leave them for 8 00:00:22,180 --> 00:00:25,390 you to read about in the docs linked in the teacher's notes. 9 00:00:25,390 --> 00:00:28,620 Okay. So let's setup some of the info that we 10 00:00:28,620 --> 00:00:31,180 want to have inserted into our database. 11 00:00:32,310 --> 00:00:37,390 Like I said, we're gonna use a dictionary to hold on to all the data. 12 00:00:37,390 --> 00:00:38,090 And the reason for 13 00:00:38,090 --> 00:00:43,300 that is because a dictionary is easy to look back to, it's easy to update. 14 00:00:43,300 --> 00:00:44,370 We can play around with it. 15 00:00:44,370 --> 00:00:48,080 We've all had a lot of experiences with databases or dictionaries. 16 00:00:48,080 --> 00:00:50,310 So, not anything crazy. 17 00:00:50,310 --> 00:00:55,830 So, I'm actually gonna make a list for all the students and 18 00:00:55,830 --> 00:00:57,860 then a dictionary per student. 19 00:00:57,860 --> 00:01:04,110 So, let's make a username and then I'm gonna say, 20 00:01:04,110 --> 00:01:10,941 Kennethlove, and we'll say points and 4888. 21 00:01:10,941 --> 00:01:11,802 All right. 22 00:01:11,802 --> 00:01:14,622 And then username. 23 00:01:14,622 --> 00:01:17,702 Chalkers, points. 24 00:01:17,702 --> 00:01:22,463 [BLANK_AUDIO]. 25 00:01:22,463 --> 00:01:23,823 Username. 26 00:01:23,823 --> 00:01:31,162 [BLANK_AUDIO] 27 00:01:31,162 --> 00:01:35,245 If you've explored the collections library, 28 00:01:35,245 --> 00:01:41,290 then this is actually a really great place to use something like named tuple. 29 00:01:44,390 --> 00:01:49,480 This is also something where we probably could have done a dictionary where 30 00:01:50,710 --> 00:01:53,920 each key is the username, and the points are their value. 31 00:01:55,130 --> 00:01:57,830 But this is a little bit more obvious of how we're going to 32 00:01:57,830 --> 00:02:01,880 put things into the database in the end. 33 00:02:04,000 --> 00:02:06,430 And [INAUDIBLE]. 34 00:02:06,430 --> 00:02:08,310 Dave has a lot of points. 35 00:02:09,330 --> 00:02:10,410 All right. 36 00:02:10,410 --> 00:02:15,400 So, here's our dictionary or our list of dictionaries, rather. 37 00:02:15,400 --> 00:02:18,800 So you can see, we've got two keys for every one of them, we've got username key 38 00:02:18,800 --> 00:02:22,200 and we've got a points key for each one of them, and then they have a value that has 39 00:02:22,200 --> 00:02:24,870 their username or their points, depending on which key it is. 40 00:02:26,250 --> 00:02:30,710 So, let's add a function that will go through all of these, 41 00:02:31,960 --> 00:02:35,750 and add them to our database, and let's call it add students. 42 00:02:39,440 --> 00:02:45,100 And what we wanna do in here is, we wanna go through each student in students, and 43 00:02:45,100 --> 00:02:49,450 we wanna use our model, which is called Student, and I'm gonna call create. 44 00:02:49,450 --> 00:02:56,465 And we're gonna say the user name is equal to the student username. 45 00:02:57,910 --> 00:03:03,204 And the points is equal to the student points. 46 00:03:03,204 --> 00:03:11,500 All right, so now let's come down here to our if name block and 47 00:03:11,500 --> 00:03:17,062 let's add that in, so down here we wanna call add students. 48 00:03:17,062 --> 00:03:19,080 'Kay. 49 00:03:19,080 --> 00:03:22,413 So, let's try running this. 50 00:03:22,413 --> 00:03:27,540 Python students.py. 51 00:03:27,540 --> 00:03:28,880 Okay, no problems. 52 00:03:28,880 --> 00:03:34,230 This is all great and wonderful until we run it again. 53 00:03:35,730 --> 00:03:37,650 Look at this. We've got all, we got tons and 54 00:03:37,650 --> 00:03:39,150 tons of these integrity errors. 55 00:03:39,150 --> 00:03:40,130 Now, why did we get those? 56 00:03:40,130 --> 00:03:45,247 We got those because, remember we made our username unique. 57 00:03:46,500 --> 00:03:50,250 And what we're doing down here is a create. 58 00:03:50,250 --> 00:03:53,451 We're saying, put in a new row for every single one of these. 59 00:03:53,451 --> 00:03:57,460 So the first time through it puts in that new row for Kenneth Love. 60 00:03:57,460 --> 00:03:59,780 And everything's cool, everything's copacetic. 61 00:03:59,780 --> 00:04:02,880 The second time it comes through, it says hey, put in a row for 62 00:04:02,880 --> 00:04:07,250 Kenneth Love, and the database says, no wait, I can't, I already have 63 00:04:07,250 --> 00:04:12,180 a row where the username is Kenneth Love, I can't do a new one, because it's unique. 64 00:04:12,180 --> 00:04:13,110 So let's change this, and 65 00:04:13,110 --> 00:04:18,760 the way that we're gonna change this, is we're gonna do a tri-block. 66 00:04:18,760 --> 00:04:19,260 Oops. 67 00:04:23,070 --> 00:04:24,040 So we can indent that. 68 00:04:24,040 --> 00:04:30,030 So we're gonna try to insert the, the student. 69 00:04:30,030 --> 00:04:33,270 And you notice that we're getting this integrity error down here in our 70 00:04:34,410 --> 00:04:35,280 error messages. 71 00:04:35,280 --> 00:04:37,840 So we're gonna except IntegrityError. 72 00:04:39,200 --> 00:04:44,821 So if we get an integrity error, then let's do student_record. 73 00:04:44,821 --> 00:04:53,590 Oops, student_record equals Student.get, username equals student, username. 74 00:04:55,880 --> 00:05:01,530 Right, so we wanna get that student, it must exist, so we wanna get that student. 75 00:05:01,530 --> 00:05:06,640 And then we want to do student_record.points equals 76 00:05:06,640 --> 00:05:07,640 student points. 77 00:05:08,770 --> 00:05:14,090 So set their points to the new one, and then student_record.save. 78 00:05:14,090 --> 00:05:16,840 So, get the student out of the database. 79 00:05:16,840 --> 00:05:19,560 Change the points to whatever the points is now. 80 00:05:19,560 --> 00:05:21,570 If it's changed, it may not have changed. 81 00:05:21,570 --> 00:05:24,820 But just set it to whatever it is now, and then save the record. 82 00:05:24,820 --> 00:05:26,100 Now we can make this smarter, 83 00:05:26,100 --> 00:05:31,130 as I just mentioned, by making it check to see if the points is different. 84 00:05:31,130 --> 00:05:32,730 I'm gonna leave that up to you. 85 00:05:32,730 --> 00:05:35,970 But that would be a good thing to do. 86 00:05:37,250 --> 00:05:42,430 All right, so now, let's add another new function. 87 00:05:42,430 --> 00:05:44,740 Actually, let's go ahead and test this one first. 88 00:05:45,780 --> 00:05:46,470 Let's do Save. 89 00:05:46,470 --> 00:05:47,380 Let's come back down here. 90 00:05:47,380 --> 00:05:48,458 We should be able to run this again. 91 00:05:48,458 --> 00:05:52,610 No complaints, great. 92 00:05:52,610 --> 00:05:55,380 'Kay, so now let's make a new function that is 93 00:05:55,380 --> 00:05:58,090 going to get our top performing student. 94 00:05:58,090 --> 00:06:02,550 I want the student that has the, the highest number of points. 95 00:06:02,550 --> 00:06:06,118 So we'll call this top student and 96 00:06:06,118 --> 00:06:11,760 we'll say student equal student, our modal, .select. 97 00:06:11,760 --> 00:06:15,300 So what that does is that gets all of the students. 98 00:06:15,300 --> 00:06:18,390 We've got all of the student records that are in the database, 'kay? 99 00:06:18,390 --> 00:06:21,220 We don't want all of them, we just want the best one. 100 00:06:21,220 --> 00:06:25,650 So let's do an order by, so let's sort them and 101 00:06:25,650 --> 00:06:31,660 we're gonna sort them by the points attribute, right? 102 00:06:31,660 --> 00:06:34,260 We wanna sort by whoever has the most points. 103 00:06:34,260 --> 00:06:37,390 And we wanted to do this in descending order, so 104 00:06:37,390 --> 00:06:41,730 we want the biggest number first, the smallest number last. 105 00:06:41,730 --> 00:06:44,750 Ascending would be the other way around where we have the smallest number first 106 00:06:44,750 --> 00:06:45,970 and the biggest number last. 107 00:06:45,970 --> 00:06:48,040 So descending, it gets smaller. 108 00:06:48,040 --> 00:06:49,600 Ascending, it gets bigger. 109 00:06:51,820 --> 00:06:54,920 So let's add one more thing to this. 110 00:06:54,920 --> 00:06:58,960 What we can do is we can say .get on the end of this. 111 00:06:58,960 --> 00:07:03,150 And that's going to only get us the first record that comes back. 112 00:07:03,150 --> 00:07:04,700 So we've got all the students. 113 00:07:06,050 --> 00:07:08,410 Sort them all by their points so 114 00:07:08,410 --> 00:07:12,020 that the biggest number comes first, and then get the first record. 115 00:07:12,020 --> 00:07:16,030 I know there's a lot there, but that's what it is. 116 00:07:16,030 --> 00:07:16,550 Okay. 117 00:07:16,550 --> 00:07:21,331 So, let's return a student.username. 118 00:07:21,331 --> 00:07:21,872 Actually, you know what? 119 00:07:21,872 --> 00:07:24,211 Let's just return student. 120 00:07:24,211 --> 00:07:25,390 Okay? 121 00:07:25,390 --> 00:07:30,830 And so, what I wanna do is down here, after I add the students, 122 00:07:30,830 --> 00:07:36,810 I want to print our top student right now is. 123 00:07:38,650 --> 00:07:44,938 And then I'm gonna do a format here where I call top_student. 124 00:07:44,938 --> 00:07:52,930 And so let's actually go here and do 0.username. 125 00:07:52,930 --> 00:07:56,530 Cuz zero is whatever comes back from this, and 126 00:07:56,530 --> 00:08:00,053 we know that has a username attribute so we're gonna print out 0.username. 127 00:08:01,482 --> 00:08:05,160 All right, so moment of truth, let's give this a try. 128 00:08:09,740 --> 00:08:10,240 oh. 129 00:08:11,360 --> 00:08:13,610 I forgot a parenthesis. 130 00:08:13,610 --> 00:08:14,660 All right, now let's try it. 131 00:08:14,660 --> 00:08:18,300 Our top student right now is Dave Mcfarland. 132 00:08:18,300 --> 00:08:19,240 Sweet. 133 00:08:19,240 --> 00:08:20,430 Dave's got a lot of points. 134 00:08:22,180 --> 00:08:23,100 Let's go change that. 135 00:08:23,100 --> 00:08:26,840 Dave's got a whole bunch of points, but I wanna be the top student. 136 00:08:28,300 --> 00:08:32,525 So, whatever Dave has, plus 1. 137 00:08:33,780 --> 00:08:36,850 Okay, so now if I run this, it should come back as me being the top student. 138 00:08:38,850 --> 00:08:39,980 Great, it did. 139 00:08:39,980 --> 00:08:41,720 Now why did it change to me? 140 00:08:41,720 --> 00:08:45,350 Just in case you didn't catch this part yet, it's because of this. 141 00:08:45,350 --> 00:08:46,420 We had the integrity error, 142 00:08:46,420 --> 00:08:50,750 there's already a row for me, so now we grab my student record, 143 00:08:50,750 --> 00:08:54,450 update my points to whatever's in the dictionary, and we save it again. 144 00:08:54,450 --> 00:08:58,430 Then when we run top student, it's gonna select my record. 145 00:08:58,430 --> 00:08:59,300 So that's pretty awesome. 146 00:08:59,300 --> 00:09:01,170 We're able to get our students from the dictionary. 147 00:09:01,170 --> 00:09:02,780 Stick them into the database. 148 00:09:02,780 --> 00:09:06,220 Select them, order them, pick out just one top one. 149 00:09:06,220 --> 00:09:10,450 And then we were even able to update and change those records afterwards. 150 00:09:10,450 --> 00:09:15,400 This work, creating, reading, updating, and deleting records is known as CRUD. 151 00:09:15,400 --> 00:09:19,000 It's the backbone of pretty much any application that deals with a lot of data. 152 00:09:19,000 --> 00:09:21,460 I can see you're getting antsy to build something real already. 153 00:09:21,460 --> 00:09:25,280 Well, that's great because our next step is to start the real reason we're here. 154 00:09:25,280 --> 00:09:27,210 A command line diary application. 155 00:09:27,210 --> 00:09:28,350 We'll do that in the next stage.