#100DaysOfCode in Python Transcripts
Chapter: Days 79-81: Basic Database Access with SQLite3
Lecture: Demo: Script to populate the address book

Login or purchase this course to watch this video and the rest of the course contents.
0:00 So what just saw in the last video,
0:02 inserting data line by line,
0:05 on this Python shell, within this Python shell,
0:07 is actually quite tedious, right?
0:09 Imagine trying to enter lots and lots of data.
0:12 Well, you're not going to do it that way.
0:14 That was just for demonstrations.
0:16 So, what I've done is I have actually written a simple
0:20 populate_db.py Python file,
0:23 which again is in the materials for this course,
0:26 and what it does it actually prompts you to enter the data
0:32 as it is required,
0:33 and you can run it as many times as you want.
0:35 It actually keeps looping through until you quit out of it.
0:38 So, let's take a look at that file here.
0:40 Now, let's create it.
0:41 You're again going to use some magic here.
0:44 You can just copy the file from the actual repo, otherwise,
0:48 just feel free to pause and tuck this in if you're crazy.
0:51 Uh, alright.
0:52 Let's create the file here.
0:55 Let's save the file as
0:58 pop, woops, populate_db.py
1:02 Alright, here comes the magic.
1:05 Alright, let's take a look at that.
1:07 Now, this is nowhere near as complex as
1:09 the generator one we did before.
1:12 So, first things first,
1:13 as soon as you click on this or run this script,
1:16 it's going to run the enter_details function, okay,
1:19 and the inter details function simply starts a while loop
1:24 and while true, which is always true, right?
1:26 Running the script is true.
1:28 So while true, it creates an info list,
1:33 an empty one, right?
1:34 We need to set this here because we are going to to add to it,
1:37 in a second.
1:38 Now, it actually runs three inputs and this is where it
1:42 prompts you to enter the data that you want in the database.
1:46 So, name=, or name is input,
1:50 enter a name.
1:51 So, when you enter a name,
1:53 this prompt is assigned to the name variable.
1:55 Same with address, same with phone number.
1:57 Nice and simple.
1:58 Now, for I, we are going to run a for loop for I,
2:02 in name, address, number.
2:05 That's the three variables.
2:07 So, it's going to iterate over them.
2:09 We want to append I,
2:14 so append the name for I,
2:16 so, for name let's just break it down.
2:18 So, for name in name here,
2:24 we are going to append the data within the name
2:28 to the info list.
2:30 Okay, so by running this for loop is where we are
2:32 populating the info list with these three variables.
2:35 Simple as that.
2:36 Now, you remember this from the generator before?
2:40 We have a with statement, okay,
2:42 so it's opening the connection, right,
2:45 and within this width statement
2:48 it is running connections on cursor,
2:50 and then it's executing this SQL here.
2:54 Now, this is the important part.
2:56 So, we are inserting into our details table
3:00 these three values,
3:02 but we are not actually.
3:03 These are actually wild cards, right,
3:07 so these are substituted just like you would with a stream,
3:11 okay, using in the print statement,
3:13 but with substituting the contents of info.
3:17 So this is very manual.
3:20 Just think of it that way.
3:21 What if info wasn't filled with three name, address, number,
3:26 three list items?
3:28 What if it wasn't?
3:29 Well, this wouldn't work.
3:31 So this was written specifically for our address book table,
3:36 details table,
3:38 because we know it has three columns: one, two, three;
3:41 and we are going from left to right,
3:43 name, address book, phone number.
3:46 So, if again, if this info was any other way,
3:49 this would mess up.
3:50 Likewise, if someone wrote an address into enter name,
3:55 and a name in enter address,
3:58 you're going to end up putting an address into the name column
4:01 and the name into the address column.
4:03 So, just bare in mind those limitations,
4:05 but the reality is inserting the info list populated
4:11 with name, address, and number into your database
4:14 and then it simply prints data inserted to database.
4:19 Then, it asks you if you want to stop.
4:24 So, if you hit q, in any case,
4:29 it's going to break out of the script.
4:31 Otherwise, it's just going to continue on
4:34 and go back to the top and ask you the same three questions.
4:38 So, let's save this file.
4:41 Lets exit out of our shell
4:45 and let's run it Python populate_db.py
4:50 So, enter a name.
4:52 Let's just give us some white space there.
4:55 Now, the name is going to be Bob.
4:58 What's Bob's address?
5:00 Somewhere over the rainbow.
5:05 Isn't that lovely?
5:06 And a phone number.
5:09 Let's go backwards.
5:13 Alright, that's Bob's phone number.
5:14 Data inserted to database.
5:16 Hit q to quit.
5:18 Ooh, there we go.
5:20 We go back to the start.
5:21 So let's enter name, Mike.
5:24 Where does Mike live?
5:25 The US of A.
5:30 And his phone number?
5:32 As per every US phone number I see on TV 555,
5:37 something else, let's go with 3226.
5:41 Data inserted to database.
5:44 Alright.
5:46 We'll hit q to quit and we get out of the script.
5:49 Done.
5:50 Alright, and it's safely closed and everything
5:52 because the SQLite connection was wrapped
5:54 in that with statement.
5:58 Refresh our database table
6:00 and there we have the new data.
6:03 So you can take something like this script,
6:06 put it into some sort of automation,
6:09 and then you'll be able to add people,
6:12 or users, or whatever to your database.
6:15 Imagine this in a Flask script.
6:17 Pretty cool.
6:18 Alright well, enjoy that and.