 And welcome to this video from Films by Chris. That's chrisbk.com. There's a link in the description of this video. I am Chris, also with a K. Today, we are going to continue what we've been working on. So this video is part of a series. Be sure to check out the first two parts. We've been working with SQLite 3 databases, and in the first video, I showed you some basic SQLite commands that you can run within the SQLite 3 program. Then in the second video, I showed you how you can run pretty much all those commands from the prompt on your computer, from your shell. Well, today we're going to take what we've learned and put that into a script. Now, we already have two databases created here from example, example two from our first two tutorials. And I list them out here with file. You can see that they're both SQLite 3 databases. Let's go ahead and just use one of those we'll use example two, which has a table in it called people that we created and has a few entries. So if I real quick select from that all the people, you can see that we have right now five people with unique row IDs, first names, last names, and phone numbers. So let's go ahead and create a script that's going to allow people to enter new information because obviously you could type this out because you're a great programmer and know a lot about databases or at least a little bit. But you're going to have users and users who are going to want to be able to enter people into this database and they don't know all these commands. You just want them, you want to ask them what's the first name, what's the last name, what's the phone number, and you want to put it in there. So let's go ahead and do that. I'm going to use Vim as my text editor, but use whatever text editor you're comfortable with as long as it's not a Word document editor. We'll just call this enter people or enter, yeah, people, sure, .sh. And of course I'm going to start off with my shebang line saying this is a bash script and then we'll create some variables. So our database name here and I'm just going to give it the local name, the relative to where we are, but in real life you'd probably want to give it the full path and of course our script is not going to create a database. You're going to already have that created. This is for people to enter in. Although you could create a script that creates it. You could also create a script that checks, see does it exist if not create it, but we're not going that in depth. So our database name is database2.db and then the table name is going to be people. And of course you want to put these into variables at the top because you're going to call these things multiple times. And if you ever change the name of the database, you want to point to a different database or you change the table name. You don't want to change it everywhere in your script. You want to change it in one place. That's the top. Now let's go ahead and be polite and welcome our user. And then we're going to say, please, again being polite, enter user info. And then I'm going to say, read, we're going to use the read commands, get this information and read does have a option to display messages. I'm drawing a blank on what that argument is. So I'm just going to say echo dash n. So that means no new line. We're going to say first name and I'll put a space there. And then we'll say read F name. Oops. OK. I'm going to say echo dash n l last name. I'm going to say read l name. And then we're going to say echo dash n phone number. I'm going to say read phone. And then we're going to use our SQL commands. We're going to say SQLite 3. We're going to say our db. And then we're going to say in quotations here, we're going to say insert into $table values. And we're going to give it these values. We're going to say $fname comma $lname comma $phone. And I'm pretty sure I typed all that proper. And that should be it. It should enter the information in there if we've typed everything correct. So let's go ahead and save this. Of course, we have to make it executable. We only have to do this once on the system. And then since the script is in this directory, we're going to say dot slash, saying it's in this directory, this script. And it says welcome. Please enter user information. First name. We'll call this guy Billy. We'll say his last name is Peterson. And we'll say his phone number is 555 dash 555 dash. Some numbers. And here we see no column named Billy. So what did we do wrong? Let's go ahead and look at our script. And I think I know it's a quotation thing. Yeah, there should be, since these are strings, we should have them wrapped in these single quotes like that. And we made all these fields in the database text, which is not necessarily the best option. But we're just going over the basics here. So let's try that again. Let's go Billy. Or what do we call him? Peterson 555 dash 555 dash some numbers. Enter and success. So let's go ahead and select all the people from there. And you can see Billy Peterson is now in our database. Now, you may want to be entering a bunch of names here. So let's go ahead and go back into our script. And here we can go into a loop. So we can just say while, one, do. And then I'm going to take all these rows and indent them. And then I'm going to say done. Now, if we run our script, it's going to ask us for a name. I'll say Peter Bobson. Sure. Phone number, blah, blah, blah, blah, whatever. And we're not doing any checking here yet as far as making sure this information is in the correct proper format. And since they're text fields, anything should pretty much succeed. So it entered that. And then it went on to the next name. We'll say John Johnson. Phone number, sure. And then I hit Control C to get out of that. Let's go ahead and go back into our script and make it a little more full. So we're getting more into shell scripting in this tutorial than we are SQLite 3. But that's part of it. I mean, once you know the basic commands for database stuff and you know the dos and don'ts, it's writing it in whatever language you choose to write in. In this case, we're doing shell scripts. Obviously, you can write in PHP or Python or Perl or C. So let's go ahead. And what we're going to do here is real quick, I'm going to put in these two pipe symbols. And I'm actually going to put into we're going to say echo dollar sign F name entered. And then I'm going to say over here echo error. OK. And I'm not going to do anything about that. But right now, we should, if we run that script again, and we'll type in a name. I'm not even going to think of names right now. I'm just going to enter stuff. It says that person was now entered. And it moves on to the next name. Let's go ahead and go in our script and make it a little bit further along. We're going to say echo, would you like to enter another user? Read. And let's go ahead and do it like this. We'll say capital Y and we will say, let's go ahead and do just a formatting thing, no new line there. And let's put a semicolon and we'll read A and S for answer. And then we're going to say, if dollar sign A and S equals, or sorry, we should do equals Y, let's do N first. I'm not going to go into depth on this because we could write a lot here. So if the user enters a lowercase N, we are going to then just echo, goodbye, and then exit. And then we're going to say, else, we'll clear the screen and that's pretty much it. We'll start to loop over again. So this could be, obviously, for real programming, basically I'm saying if you click N, a lowercase N, it's going to exit out of the program. And if you do anything else, it's going to default to yes, which the capital Y here means Y is the default. So if you left it blank or put Y, it's supposed to continue. You really should put in something else. If you hit something else, say sorry in valid answer. But let's go ahead and just go with that. And we'll run this script. And I'm going to clear this, oh, I can't clear the screen once I start the script. Okay, we're going to say John Peter, Peter, again, I think there's already one of those in there. We'll give him a phone number. And it says, John's entered. And it says, would you like to enter? If I hit anything other than N, it's going to start over again. Now I can type in Peter Parker. He has a phone number, enter. And now if I hit N, it should say goodbye and exit out of the program. Okay, so the only thing you really want to get into here is checking if these values are proper. If you can do something simple as if any of these are blank. So we can do one check or multiple check. I'll just do a quick here. I'll say if dollar sign F name is blank. I'll just do this on one line. Then echo value needed. And then we will exit with a one. Actually, I said one down here and this should be a zero because that's a proper exit. This is an improper exit. Okay, and what I'm going to do here is I'm going to quickly just paste that there. There's better ways to check this, but this is a quick, simple way to do this. You can check. You want to check not only if they filled it in, but if it's the right format. And here we'll say phone. So in theory, if I typed all that right, let's go ahead and type one improper. Again, we'll say Frank Castle and give them a number. Frank was entered and I'll hit enter to continue. Now I'll just hit enter and it says value needed and it exits out. If I run it again and I give it a first name, Peter and I leave the last name blank. It's going to say value needed and exit. I can say Peter Parker. If I leave the phone number blank, it's going to say value needed. But if I say Peter Parker and give it a number, Peter was entered. I'm going to say no to continue. Anyway, these are some things very, very basic. Obviously, when you're writing an actual program for people to use you, the hardest part of programming is thinking of what the end user is going to do that you're not thinking of and there's always something. But I thought I'd give you some examples here. I will throw this up on Payspin and hopefully put a link in the description of this video to it. And I hope that you found this useful. I do thank you for watching. I'm going to do at least one more video on SQLite, playing around with the files, some things you may or may not know, things you might think about. And this video was actually more on scripting but scripting to use that SQLite database. I do thank you for watching. Please visit filmsbychrist.com. That's Chris Decay. There's a link in the description and I hope that you have a great day.