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


Talk Python's Mastodon Michael Kennedy's Mastodon