1 00:00:00,000 --> 00:00:04,398 [MUSIC] 2 00:00:04,398 --> 00:00:07,070 Well, we have a table and a menu for actions. 3 00:00:07,070 --> 00:00:10,700 Let's write the function that'll actually let us add entries to the database. 4 00:00:10,700 --> 00:00:14,670 Python comes with a library named SYS that relates to system functionality on 5 00:00:14,670 --> 00:00:15,210 your computer. 6 00:00:15,210 --> 00:00:18,627 We're gonna use a great feature of that library, STDIN, so 7 00:00:18,627 --> 00:00:21,579 that we can capture an entry that has new lines in it. 8 00:00:21,579 --> 00:00:25,634 Since we're going to be using the sys library, we should probably go ahead and 9 00:00:25,634 --> 00:00:26,258 import it. 10 00:00:26,258 --> 00:00:28,543 [BLANK_AUDIO] 11 00:00:28,543 --> 00:00:30,340 Not that it's really important, but 12 00:00:30,340 --> 00:00:33,935 I'd like to point out that we have the stuff that comes from Python here, and 13 00:00:33,935 --> 00:00:38,080 then a blank line, and then the stuff that comes from a third-party library. 14 00:00:38,080 --> 00:00:41,890 This is a really good and really common way of organizing your imports. 15 00:00:41,890 --> 00:00:44,490 I suggest that you kinda stick to it. 16 00:00:44,490 --> 00:00:45,070 Okay. 17 00:00:45,070 --> 00:00:48,960 Let's come down here and check out our Add Entry function. 18 00:00:48,960 --> 00:00:51,160 Cuz this is where we're gonna use sys. 19 00:00:51,160 --> 00:00:54,662 So, it's gonna capture everything that the user types, 20 00:00:54,662 --> 00:00:59,680 until they enter what's known as an End of File key sequence, an EOF key sequence. 21 00:00:59,680 --> 00:01:04,880 The way that you get this End of File is by pressing CTRL and D at the same time. 22 00:01:05,940 --> 00:01:08,427 So, let's tell the users that. 23 00:01:08,427 --> 00:01:17,909 So print, Enter your entry, press CTRL D when finished. 24 00:01:17,909 --> 00:01:19,480 Kay. 25 00:01:19,480 --> 00:01:22,770 Now, we need to actually capture their input. 26 00:01:22,770 --> 00:01:26,810 And this is where the STDIN that I talked about comes into play. 27 00:01:26,810 --> 00:01:28,260 So let's call this data, 28 00:01:28,260 --> 00:01:32,890 and it's gonna be sys.stdin, which is just like the keyboard. 29 00:01:32,890 --> 00:01:35,360 And then read, bringing in all the data, 30 00:01:35,360 --> 00:01:39,870 everything that comes in from here, and then, oops, and then strip. 31 00:01:39,870 --> 00:01:41,720 Get rid of any of the white space on either side of that. 32 00:01:42,730 --> 00:01:49,160 Now though, we need to make sure that we actually got something from them. 33 00:01:50,480 --> 00:01:52,180 So let's say if data. 34 00:01:53,190 --> 00:01:54,300 So cool, we've got data. 35 00:01:54,300 --> 00:01:56,370 Something actually came in. 36 00:01:56,370 --> 00:01:59,770 And then, let's make sure that they want to save this. 37 00:01:59,770 --> 00:02:04,510 So if input save entry? 38 00:02:04,510 --> 00:02:09,130 And we're gonna have a Y and an N, and then we're gonna lowercase that. 39 00:02:10,340 --> 00:02:16,670 So if that comes in, and it doesn't equal n, input comes in, 40 00:02:16,670 --> 00:02:21,830 they answer with anything other than n upper case or lower case doesn't matter, 41 00:02:21,830 --> 00:02:25,950 but anything other than the letter n [LAUGH], then we wanna do something. 42 00:02:25,950 --> 00:02:29,785 And what we wanna do is we wanna call entry.create cuz we're creating a new 43 00:02:29,785 --> 00:02:30,950 entry, right? 44 00:02:30,950 --> 00:02:32,000 Did you forget about the model? 45 00:02:32,000 --> 00:02:33,300 I almost did. 46 00:02:33,300 --> 00:02:37,980 And we're gonna say the content of our entry is the data that they just typed. 47 00:02:39,130 --> 00:02:42,530 And then let's print out saved successfully. 48 00:02:44,930 --> 00:02:46,250 And that's it. 49 00:02:46,250 --> 00:02:47,850 We don't have to do anything else. 50 00:02:49,520 --> 00:02:51,370 Well we do have to add another blank line there. 51 00:02:52,940 --> 00:02:55,400 So we're creating an instance for printing out a message. 52 00:02:55,400 --> 00:02:59,110 I'm pretty sure that covers all the stuff we have to do on adding an entry. 53 00:02:59,110 --> 00:03:03,230 So let's try it. 54 00:03:03,230 --> 00:03:04,947 Come right down here ./diary. 55 00:03:04,947 --> 00:03:06,190 Oh, I love that. 56 00:03:06,190 --> 00:03:07,358 So much better. 57 00:03:07,358 --> 00:03:09,850 All right, and let's say A for add an entry. 58 00:03:10,990 --> 00:03:12,150 And I'm gonna my entry. 59 00:03:12,150 --> 00:03:21,391 So I'm gonna say working with databases has been really neat. 60 00:03:21,391 --> 00:03:25,460 I'm glad I can show this off. 61 00:03:28,540 --> 00:03:31,988 I can even have multiple lines. 62 00:03:31,988 --> 00:03:32,540 Okay. 63 00:03:32,540 --> 00:03:34,600 And then I hit CTRL and D. 64 00:03:35,930 --> 00:03:37,940 It comes back with save entry. 65 00:03:37,940 --> 00:03:40,050 So I'm gonna say yes. 66 00:03:40,050 --> 00:03:41,070 Actually I can just press Enter. 67 00:03:42,160 --> 00:03:44,770 And it came back with Saved Successfully. 68 00:03:44,770 --> 00:03:46,470 That's great. 69 00:03:46,470 --> 00:03:48,730 Let's try doing one where I don't wanna save it. 70 00:03:48,730 --> 00:03:51,115 I don't want to save this entry. 71 00:03:51,115 --> 00:03:54,940 Ctrl+D, no don't save that. 72 00:03:54,940 --> 00:03:56,030 It just goes straight back. 73 00:03:56,030 --> 00:03:56,910 I didn't save anything. 74 00:03:56,910 --> 00:03:58,770 It just takes me straight back to the menu. 75 00:03:58,770 --> 00:04:00,580 I think this is actually working really, really well. 76 00:04:00,580 --> 00:04:03,510 Now that we can add things, we need to be able to see them. 77 00:04:03,510 --> 00:04:04,740 Let's tackle that in the next video.