1 00:00:00,620 --> 00:00:05,543 Now that we have our database created, it's time to start making some entries. 2 00:00:05,543 --> 00:00:11,132 First, if you would like to clear up the console a bit you can set echo to False. 3 00:00:12,676 --> 00:00:17,820 This will remove a lot of the additional console messages when running the file. 4 00:00:17,820 --> 00:00:21,570 You're more than welcome to leave this message turned on if you'd like. 5 00:00:22,880 --> 00:00:25,490 Our first entry will be you. 6 00:00:26,530 --> 00:00:30,420 You can also copy my code and add me to your database if you'd rather. 7 00:00:32,140 --> 00:00:37,171 Let's jump down here to the bottom, add a space and 8 00:00:37,171 --> 00:00:42,560 create your variable with your name underscore user. 9 00:00:43,950 --> 00:00:51,800 Set it = User, and inside we'll have name=, and then your name. 10 00:00:54,110 --> 00:00:57,082 fullname = and then your full name. 11 00:01:00,148 --> 00:01:07,358 And then nickname =, and your nickname. 12 00:01:07,358 --> 00:01:12,935 Since our ID is our primary key, we don't need to pass in a value. 13 00:01:12,935 --> 00:01:16,960 One will be created for this entry when it's added to the database. 14 00:01:20,050 --> 00:01:24,790 Now let's print out our name, 15 00:01:24,790 --> 00:01:31,170 meg_user.name, and let's do id too, 16 00:01:31,170 --> 00:01:36,092 meg_user.id, run the file, 17 00:01:36,092 --> 00:01:41,210 python3 for me cuz I'm on a Mac. 18 00:01:42,830 --> 00:01:46,380 And we have our name and then none for the ID. 19 00:01:47,510 --> 00:01:51,458 This entry hasn't been added to our database yet. 20 00:01:51,458 --> 00:01:56,139 We need to add sessions to do that. 21 00:01:56,139 --> 00:02:01,140 At the top of the file we'll add an import 22 00:02:01,140 --> 00:02:06,890 from sqlalchemy.orm import sessionmaker. 23 00:02:08,600 --> 00:02:14,657 Then after our engine, we'll need to create two variables, 24 00:02:14,657 --> 00:02:20,013 the first, session with a capital S = sessionmaker, 25 00:02:20,013 --> 00:02:23,520 inside we'll have bind=engine. 26 00:02:25,970 --> 00:02:30,620 This binds our sessions to the database. 27 00:02:30,620 --> 00:02:35,435 Next we'll add another variable called session with 28 00:02:35,435 --> 00:02:41,190 a lowercase s and it we'll call our Session, great. 29 00:02:41,190 --> 00:02:45,400 Let's add a few more things before I get into what session is doing. 30 00:02:45,400 --> 00:02:48,326 Trust me, it will all make more sense in a minute. 31 00:02:48,326 --> 00:02:51,310 Jump back down to where we created our first entry. 32 00:02:54,310 --> 00:02:59,900 Remember, it hasn't been added to the database yet, id is still none. 33 00:02:59,900 --> 00:03:03,215 Below let's add session, oops, 34 00:03:03,215 --> 00:03:09,300 not sessionmaker, session.add and add your user. 35 00:03:09,300 --> 00:03:14,970 This adds our new user to our session which communicates with the database, 36 00:03:14,970 --> 00:03:17,550 but it's not in the database yet. 37 00:03:19,330 --> 00:03:23,800 It's kind of like putting items in your cart when shopping online. 38 00:03:23,800 --> 00:03:29,310 You've added them to be purchased but you haven't committed to them yet. 39 00:03:29,310 --> 00:03:35,214 You can change items, you can remove items and when you're ready to check out, 40 00:03:35,214 --> 00:03:39,990 you give a final Place Order to finally commit to ordering them. 41 00:03:41,340 --> 00:03:45,920 So right now our user is in this placed in cart stage. 42 00:03:45,920 --> 00:03:48,720 We haven't committed to adding it to the database yet. 43 00:03:49,780 --> 00:03:54,730 We can see that it's inside of our session 44 00:03:54,730 --> 00:03:58,838 by printing out, session.new. 45 00:03:58,838 --> 00:04:04,799 Run the file and there's our user. 46 00:04:04,799 --> 00:04:07,817 Let's finally add this entry to our database. 47 00:04:07,817 --> 00:04:16,560 Replace this print statement with session.commit. 48 00:04:16,560 --> 00:04:19,610 This will add our user to the database. 49 00:04:19,610 --> 00:04:25,805 We can check by printing out our id again, 50 00:04:25,805 --> 00:04:30,762 meg_user.id, run the file and 51 00:04:30,762 --> 00:04:35,570 our id goes from none to 1. 52 00:04:35,570 --> 00:04:38,416 That means it was added to our database. 53 00:04:38,416 --> 00:04:41,600 We can also add multiple users at once. 54 00:04:41,600 --> 00:04:47,600 You can use a session.add_all. 55 00:04:49,320 --> 00:04:54,330 Since we've already added this user to our database, let's comment this out. 56 00:04:55,910 --> 00:05:02,164 Cmd or Ctrl+/ will comment out a group of code all at once. 57 00:05:05,514 --> 00:05:10,109 Now inside of this function, we're going to pass in a list of users. 58 00:05:10,109 --> 00:05:14,765 I have a list in the teachers notes if you wanna copy and 59 00:05:14,765 --> 00:05:19,215 paste them or create a list of users of your own, and 60 00:05:19,215 --> 00:05:24,095 paste and we have our users Grace, Catherine and Alan. 61 00:05:24,095 --> 00:05:27,693 You can also make this list a variable instead. 62 00:05:39,072 --> 00:05:42,836 And then pass in the variable into add_all. 63 00:05:48,897 --> 00:05:53,995 Also need a session.commit, and 64 00:05:53,995 --> 00:06:01,392 let's loop through our users so we can see our id. 65 00:06:15,456 --> 00:06:20,622 So for user in our new_users list, I want to print 66 00:06:20,622 --> 00:06:26,281 their id after we've committed them to the database. 67 00:06:26,281 --> 00:06:32,282 Run the file, Oops and 68 00:06:32,282 --> 00:06:37,468 I forgot a comma, comma save. 69 00:06:37,468 --> 00:06:39,235 Let's try that again. 70 00:06:39,235 --> 00:06:42,510 There we go and we see 2, 3 and 4. 71 00:06:42,510 --> 00:06:45,800 So now our database has a total of four users. 72 00:06:47,101 --> 00:06:52,321 .Another way to check if your entries have been 73 00:06:52,321 --> 00:06:59,103 added to the database is to check via the sqlite shell on a Mac or 74 00:06:59,103 --> 00:07:03,687 in workspaces type sqlite3 users.db. 75 00:07:03,687 --> 00:07:11,390 On Windows, you'll type sqlite3.exe users.db to open your shell. 76 00:07:11,390 --> 00:07:16,231 But you'll also need to download SQlite first, check the teacher's notes for 77 00:07:16,231 --> 00:07:18,210 a list of steps. 78 00:07:18,210 --> 00:07:24,730 Once you have the shell opened, type .tables and hit Enter. 79 00:07:24,730 --> 00:07:27,160 You'll see our users table. 80 00:07:27,160 --> 00:07:32,350 Next we'll need to use a sql statement to see all users in this table. 81 00:07:32,350 --> 00:07:36,138 SELECT * for all, FROM users, and 82 00:07:36,138 --> 00:07:42,090 then don't forget that ; to finish out the statement. 83 00:07:43,500 --> 00:07:46,376 And they're all of our users. 84 00:07:46,376 --> 00:07:49,542 Using DB Browser for SQlite, 85 00:07:49,542 --> 00:07:54,110 you can now see all four users in our table. 86 00:07:56,030 --> 00:07:58,080 A lot of knowledge dropped in this video. 87 00:07:58,080 --> 00:07:59,870 So let's do a quick recap. 88 00:08:02,170 --> 00:08:06,670 You create an entry to the database by calling the model class and 89 00:08:06,670 --> 00:08:10,085 passing in the values for each column except id, 90 00:08:10,085 --> 00:08:15,810 which will be created for you when the entry is added to the database. 91 00:08:15,810 --> 00:08:19,195 We use sessions to keep track of our additions like 92 00:08:19,195 --> 00:08:23,580 a cart keeps track of the items you want to purchase. 93 00:08:23,580 --> 00:08:28,612 You then commit these additions to the database by calling session.commit. 94 00:08:28,612 --> 00:08:33,810 Nothing is added to our database until we call session.commit. 95 00:08:33,810 --> 00:08:36,710 Until then, it's just sitting inside of our session. 96 00:08:36,710 --> 00:08:42,520 You can also add a group of users all at once by using add_all. 97 00:08:45,350 --> 00:08:46,700 Nice job. 98 00:08:46,700 --> 00:08:53,410 Take some time to review the code so far, and ask yourself what each line is doing. 99 00:08:53,410 --> 00:08:56,330 Review the videos as many times as you need.