 Right, so hey guys and welcome back to another Python tutorial So today's video is going to be the start of a very interesting series Which is going to be creating a college management system using Python So just to let you guys know this system is going to be a command line based application Which means it's not going to have a graphical user interface. We might do a series on that in the future So this series is going to actually be using a MySQL database to store all information Retrieve information, edit information and delete information related to the college system Cool, so this is based on what we learned in my previous tutorials about how to use MySQL and manipulate a database using Python So I'm going to be linking a couple of tutorials in the description if you are not familiar with MySQL So go ahead and watch those first and if you have a bit of an idea of how MySQL works, you can carry on with me now So first up what we want to do is make sure that you have ZAMP installed, so open browser and type in ZAMP download So click on the first link and then it should take you to Apache friends website So you want to click on the most recent version of PHP and then download the Version of ZAMP that is compatible with your system So ZAMP is going to let us pretty much host the MySQL database and let us interact with our database Cool. So once you've got that installed, you can go in your Windows search bar and type in ZAMP And then it will come up with a pop-up saying ZAMP control. So that's what you want to open up So I'm going to go ahead and open mine up, which is already in my Taskbar down here So I'm going to click that and this is what it looks like So once you have it open what you want to do is click on start the Apache server because that's going to let us have the GUI version of MySQL and then click on start for the MySQL so that we can interact with our database Cool. So the second requirement for this is to actually have a hyphen module for this installed as well So what you want to do is open up cmd or command prompt Make sure you have pip installed and then you type in pip install MySQL hyphen connector hyphen hyphen and press enter Now mine already says requirement already satisfied because I've already got this installed So if you guys haven't got this installed already make sure you run this command and have it installed successfully So once you're done with that our setup is then complete So what I'm going to do first of all is go ahead and open up MySQL admin so that I can Create the database for our system. So we're going to be working backwards for our system cool So let's go ahead and take a look at how this works So this is the user interface for MySQL if you don't want to write queries and you want to do everything using the GUI So in the last tutorial, I did show how to use queries, but in this tutorial just to save time We're going to be using GUI instead of the queries. So I'm going to click on new and open a new database We're going to call this database college because it's a college management system So database is going to be called college. Let's create that up Now in our college database, we can have a couple of tables. Okay, I'm opening the wrong one So I click on college and it says no tables found because quite rightfully we have no tables in here So we're going to have a couple of tables in here. Our first table is going to be called users So this table is pretty much going to store all the users that us that exist on our server So it will store the username. It will store the password and it will also store the privilege So whether it's a student account or a teacher account because our College management system is going to allow an account for teachers and an account for students Cool. So let's open this up and click on go So once you click on go, it will take a second to load and it will let you create fields for your table So I'm going to create an ID field which is going to be an automatically incremented field So if I go to my right side, there is an AI field up here I want to take that box to make sure this is automatically incremented So what this means is that it's automatically increments the ID by plus one each time a new record is added Cool. So I'm going to go back. That's my ID done. Don't change anything else in there Now in my users database I also want to have a username so that we can refer to the user I'm going to saving I'm going to be saving that as a rocker or a Variable character and then 255 because that's just the max for it if you want to go with good practice you would obviously go with Something about I would say 25 to 30 because that's the max limit for a username and that should be okay But we're just going to be creating this for fun as like a beginners project. So that's fine I'm going to create a password field and I'm going to be saving this as a variable character as well again, I'm going to go with the max so that we don't run into any errors for now and Lastly as I said, we're going to have a privilege Field which is going to be a variable character as well because we're going to be storing strings in there and this variable Privilege field would either store a teacher or a student string which will signify what type of an account it is let's save this up and Once I've saved if I open up my college database now I have a users table in there, which is where my user accounts for all the people are going to be stored Now we're going to have an admin account on this console application. That's going to be able to register student or teacher accounts So essentially this admin account would be the technician who has all the privileges and is able to register anyone Now our privilege will specify whether the account has more privilege than the other because students are going to have less Privileges than the teachers obviously teachers will be able to take registers and all that stuff Whereas students will be able to do and not a lot of admin stuff anyway So that's our basic stuff done, which is enough for this tutorial So we're only going to be doing up to allowing the user to allowing the admin to log in allowing the You admin to create a teacher account allowing the admin to create a student account And allowing the admin to delete any of those accounts. So that's what we're going to be covering in today's tutorial So let's go ahead and create our file now because we've done the back-end ish of our Of our tutorial today. So I'm going to go ahead and create a new text document and call this college System and then I'm going to change the extension to pi because we're coding in Python obviously Now let's open this up in visual studio. So let's open up visual studio code I forgot I could do that in there and then let's just drag this in here. Cool. So we have that done now So we can actually start coding our application now So first off what we want to do is actually import what we need. So we need only the MySQL module in this So I'm going to do import mysql.connector which is the class that lets us connect to our MySQL database As MySQL so that we can refer to it easily now. We're going to initialize our database in here So database equals so db stands for database. It's just a variable and we're going to use our MySQL object that we just initialize .connect which is one of the methods it has and then we need to provide it with our host which is going to be local host as Default your MySQL database will be hosted on these parameters user is going to be root unless you've changed it password is going to be blank by default and my database as I know is called college because if you go back to my phbMyAdmin The database name is college because we called it that Cool. So once that's done that just means my database has connected successfully Now I'm not going to be adding any try and accept in here But if you want to you can do it for good practice. I'm going to also initialize a command handler variable in here Which is going to be an object of Database.cursor. So this cursor is pretty much going to allow us to run different queries such as delete insert edit or so on So I'm also going to pass in buffered equals true so that we can run multiple queries on this without having any errors Cool. Now the first function that we actually want to program in here is going to be called our main function So our main function is going to be holding the main menu for our program So main and then this is going to be an infinite loop. So while one We're going to print out a menu for the user. So we're going to say a nice message. Welcome to the college system We're going to print a blank line. I know I can do forward slash end But it's just easier for me to speech box one off. I'm going to say option one is going to be Login as a student I'm going to copy this line and paste it down here. I'm going to change the option one to option two and I'm going to say login as teacher and Then lastly, we're going to print that line again, but the option is going to change three and we're going to say log in as admin So these are the three options that our main menu is going to have so every time the user logs in into or opens up our Application, this is the main menu is going to be greeted to or if they log off in account So now we're going to have to take the input. So user option equals input string Option or let's say. Yeah, let's just say option. So the user will answer the number Corresponding to the option they want to select. So if they wanted to log in as teacher, they would type in two and so forth So now we're going to have to write an if conditional for this. So if user option equals one That means they want to do a student login. Okay, let's just scroll down a bit here That means they want to do a student login. So I'm just going to type student login over here Now I'm going to also do an elive. So elive user option equals two that means the user wants to do a teacher based login so teacher login and We need to do elive user option equals three. That means they want to do an admin login Now I'm going to print admin login here just so that we can see how this works. And then lastly, I'm going to print else print No valid Option was selected So let's go ahead and run this main function down here because it's already stored as a function. We've got to run it as well main and Then let's run this to see if it actually works Cool. So we have a nice little command line application that says welcome to the college system login as student login as teacher or login as admin Let's press on one and it says student login. Now, let's press on Let's see. Let's press on two and it says teacher login and let's press on three and It says admin login. So it is recognizing the options. We're selecting and we have if I type in some gibberish in here It says no valid option was selected, which means it's working fine. Cool. Let's close this off now Insert we're only going to be programming the admin login for today because the user should be able to register Students or teacher accounts and be able to delete them. That's what the admin is able to do for the current moment in this tutorial So I'm going to get rid of admin login in here and we're actually going to assign this to a authentication function so authenticate admin so off admin which stands for authenticate admin, so we're going to create a function That's going to grab the username and password from the person trying to log in and verify whether they are the actual admin to the system or not Now if the username and password is correct They will open a session for the admin if not, they will just let the admin know that hey This user information is wrong and the admin will be redirected back to this menu. Cool So let's go ahead and code that function now. Let's put this above main so deaf Because we're creating a new function and we're going to call this off admin Authorize admin so in my authorized admin what we're going to do first is we're going to print a blank line Once again, as I said, I could do forward slash in but I just can't be asked for today So I'm going to just do that admin login is going to be the title Blins another blank line and then we're going to do a username input. So username equals input string username so pretty so far we're just doing a lot of basic stuff and just Trying to practice the initial stuff and then for the password We're taking a string input as well of Cool. So now that we've got the input from the user. We've got to match it. So if username equals Admin now by default, it's not good practice But by default I'm making sure that the admin password is saved and embedded in our program Not on our server because if it was on our server anyone would be able to change it But if it's embedded in our actual program, no one would be actually able to change it It would just be stuck there unless it's like reverse compiled, which is not the case in most Most places. So anyway, if the username equals admin, we want to authenticate the password So we say if password equals Let's just say the admin had a stupid password called password password That means the username and password is correct So we're gonna do admin Okay, let's just do admin session Which is going to be a function we create in a moment else we say print incorrect password over here and If it's the else for the other one, we're gonna say print login Retails not recognized and then That should be it Cool, so now it's going to complain that the admin session function doesn't exist So I'm gonna quickly go ahead and create a new function called admin session Def admin session. Now this function is only going to be run if the username and password provided by the admin is correct so print login success Welcome Admin cool, let's run this up to just test run this whole thing now We're obviously just log in and as an admin because we only have that function so three now It says admin login and we're being asked for a username So I'm gonna type in the username which is admin I'm gonna type in the password as password and as you see it says login success welcome admin So it's running the function that is admin session Which is fine. That's what we want it to do now Let's log in again, but we'll log in using the wrong credentials. So I'm having a jibberish username jibberish password and it says Login details are not recognized. So as you see it's working fine Cool, so now let's actually program this admin session so that admin can have a menu once he or she has logged in So we're gonna print the menu for the admin. Let's take a look So For the menu what we're gonna do is actually create a volume here because the admin should be able to pretty much be able to Access this menu on and on again When they've logged in unless they log out so unless they log out this menu will be recurring as a loop. So print admin menu and Then we're going to just copy this off paste it right here and then let's give it some options So option one Option one is going to be register new student Let's copy this Paste it here Paste and paste and paste cool. So copying and pasting is really great because it saves me a lot of time So I'm going to change the numbers to one two three four five. So there's five options Now it's going to be register new student then it's going to be register new teacher Then it's going to be delete existing student Student Okay, delete. Let's just make this three caps Delete existing student and we will do delete existing teacher So these two options will allow the user admin to either delete an existing student or an existing teacher account information Cool, and then lastly we want to log out button so that the user can go back to our other menu Just in case he doesn't want to be stuck in this menu for the whole time Cool. So what we need to do first is create a user option variable again so that we grab the user option input string Option Cool, and then we got to do our F statements to check what the user is selecting So let's do if user option Equals one. This means that they want to register a new student. So what we want to do first is print The blank line then we're going to print again register new student and In here, we need to grab the username that the admin wants to give the students so username equals Input string come on go Input string and then student username Okay, let's do you username so we need to grab the username that the admin wants to give the New student and the password that the admin wants to give the new students so they can log in later password equals input string and Student password Now these disinformation that's gathered here from the admin is going to be stored in our database in a second So once they're done, we're going to just save these values username and password into a variable called query values So we're going to type in username in there and password in there So this variable right here called query values is going to be passed in later into our query so that the query can know What the value of username and password is to be stored in our database? Cool. So what we're going to do now is go on and run our command handler Which is the object that's gonna help us one pretty much any command or queries on our SQL database so command handler execute insert into Users which was the table that we created Now we need to mention what fields we're inserting we don't need to insert ID because that's also incremented for inserting username password and Cool, and then since we've mentioned what fields will be entering we need to type in values And then we need to specify what values we're going to be passing through So the first two values which are username and password are going to be passed in as a string format So just percentage sign and s because we're going to be passing be passing them as the query Values or query valves variable right here. We're going to be passing them using that variable So just leave them as string for matters and then the last value is going to be student So this account privilege is going to be set to student because it's a student account and Lastly once the speechmark is done. You want to do a comma and pass in the query values, which is going to be Replacing this s with the actual username the user insert and replacing this second s with the actual password the user insert Now if everything was fine, we're going to do a database commits in here to save all the changes to database and then print username plus username plus a space Has been registered as a student Cool, let's run this up and see if this actually works. So scroll this up Let's press on three because we want to log in as an admin We're going to type in the admin username, which is admin and the password is password Now we have our admin menu, which is nicely showing up Obviously, we only have Register as a register new student option showing up only that at the moment. So let's press one So as we programmed it to is asking us for a student username Let's just call him mark and student password is going to be mark one two three cool, let's press enter and then it took a second and it says mark has been registered as a student And then we have an admin menu again because it's looping through and it will keep doing that until we Click on logout, which is going to be programmed later Let's actually verify if this worked by refreshing our admin page be my admin page going to our user state table and Voila, it has worked. We have an idea of one username mark password mark one two three and Privilege has been set to student because it's a student account now We're going to be doing pretty much the same for the teacher account So we can actually copy and paste which is the wonderful bit about this So the option one was creating a student account now option two is creating a teacher account for which what I'm going to do It's type in an e-lift. Yes, so e-lift user option equals to What we're going to be doing is literally copying and pasting this whole lot Because it's literally the same thing the only thing that will change is the account privilege and then these little words right here register new teacher instead and then student username will change to teacher username and teacher password Then the the rest remains the same I believe and the only bit that will change in here is where you run the query using command handler you would change the student What did I do that? You change the student to teacher now because this is a teacher account And that should be stored like that in the database and then lastly username has been registered as a teacher not as a student Let's do that run this and see if that works Okay, go up. Let's log in as admin using option 3 admin and password Let's register a teacher teacher username is going to be Matthew and it's going to be map one two three and As you see Matthew has been registered as a teacher now. Let's go ahead and open up My admin again and refresh this go ahead and see our users table and as you see the it worked It says ID to Matthew and map one two three and the difference is that this account has been privileged to a teacher account So we know how to make the distinguish between them using the privilege tab Cool. So that's working flawlessly. Let's go ahead and program the delete functions as well And then the logout button. So elif Let's go ahead and do it over here elif user option equals three That would be for deleting an existing account, which is for a student So let's go ahead and see how we're going to be doing that. So first off, we're going to print a blank line like we always did then we're going to print delete Existing student And then what we're going to be doing next is actually gathering the username So that we can delete that actual user from our database. So username equals input string Student Username So we need the students username so that we can look up the student in our users table and then delete that Specific user mentioned in here from the table so that it the account has been deleted for her and this student will no longer be able to pretty much log into that account Cool. So once that's done, we want to run in our query values variable again username is going to be stored in there as well As we're going to be storing Student as the second parenthesis the second parenthesis is stored as student because the Account privilege we're going to be looking for is a student account. So we don't want to delete any Teacher that has the same username. We only want to delete any student that has the same username Cool. So once our query very values variable is done We're going to do command handler dot execute and then let's write our query The query is going to be delete from our users table where where username Equals now we're going to use a string formatter percentage s because we're going to be providing the values in a moment and privilege equals percentage s because we're going to be providing that as a value as well Cool. Now. I'm going to go ahead and put a comma in here and pass in the query values Now once that's done I'm going to print I'm going to do a database commit to save all the changes of the deletion that's done Now we need a bit of validation here. So if command handler dot row calves Which means So what this does is it tells us how many rows were affected So if no rows were affected, it will return zero or anything less than zero So if no rows were affected that means there was no user with the username provided So that means there was no records that were deleted and no user with that username existed So that's what we want to find out whether the user with that username actually existed or not So if the user did exist So if the handler dot row count is less than one that means it didn't exist. We're just going to print out User not found Because that user does not exist. That's why no rows were affected else. We're going to print out we're going to print out username plus and Then has been Deleted cool. Now, let's quickly run this to see if it worked. It should have worked Now we're going to be deleting a student account. So let's log in as admin admin password option number three Student username. Let me see what student I had saved up. It was Mark. Okay, so let's type in Mark and it says mark has been deleted Let's go ahead and check if he has actually been deleted or not refresh and if you see right here We only have Matthew, which was a teacher account. So Mark which was a student account has been deleted through our program. Now, let's try and see if our validation is working So let's go through admin menu again click on three and Then provide a student name that doesn't exist some gibberish And it says user not found so it is working. Perfect Now as you may have already guessed we can copy and paste this function for our teacher function, too Because there's only going to be one value that changes which is the account privilege. So I'm going to copy this Then we're going to go ahead and do the elive statement. So Elif user option equals four, I believe yep four Now we paste all of this down here then we change these little words to from student to teacher So it's pretty simple what we're doing right here. We're just playing with queries. It's just a little practice Kind of tutorial for you guys to like brush up your skills with this Now you're going to change the query values to username and teacher because it's a teacher account privilege needs to be equal to teacher Where username equals that and privilege equals that which is fine User not found has been deleted. So cool. That should have worked. Let's go ahead and see if this works So I'm going to log in as admin again Just put this all the way up log in as admin username is going to be admin password Let's go on option number four because we want to do a teacher and let's Okay, quickly existing teacher account, but it's saying student teacher. Okay made a mistake there But I'll fix that in a moment. So I'm going to type in gibberish and it says user not found Let's press on option four again. Let's actually type in Matthew and it says Matthew has been deleted. Let's verify this refresh and as you see the table is actually empty my skill return an empty result zero rows So that's working perfectly. Let's just gonna I'm just going to have to change teacher student to I mean Teacher username. So we're going to go and change that right now. So Cheat chair username Cool, so that's that sorted and now the last and last but not the least function for this tutorial right here For the admin panel to be finished is the logout button, which is the easiest one So we're going to do an elif in here, which is going to be for option five. So Elif Elif user Equals five what we want to do is just break So what happens here is since we're already in a loop in our if I go to my main function We're already in the loop and when we run the other function, which is admin session We're in a second loop. So when I break from my second loop, I am returned back to my first loop So we'll be returned to this main program right here as soon as we break out So that will be like a little log on log out thing going on So that should work just fine. And then lastly, I'm going to do an else just for validation So if the user types in any gibberish, you would just say print no valid No valid option Select it. Cool. Let's run this up to see if this worked and I'm going to log in as admin because that's the only feature working currently admin and password and We're going to run the last option which is log out five And as you see right here, we have logged out from our admin because it no longer says admin menu It only says welcome to the college system, which is the main menu for the system Now if I want to log in as admin again, I'd have to press three and then type in the username and password again As we've already logged out. So that was it for today's tutorial guys Hope you have enjoyed this amazing tutorial and brushed up your skills on my skill If you guys have any future ideas for videos drop them in the community tab under my post ideas for new videos and If you guys would like to support the channel directly, you can do so by signing up as a patron using the patreon link in the description Do consider joining the discord channel for a bit of fun and programming ideas Also follow up my socials. Thank you guys once again for all the support You've been showing I am very grateful for it If you guys would like to see my channel grow I'd really appreciate you for if you could share it and ask your friends and family to subscribe and Guys, I will see your beautiful faces in the next tutorial Which is part two for this series until we finish the college system. Peace