Delete A Record From Our Database - Python Tkinter GUI Tutorial #21

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what's going on guys John the elder here for Koda be calm and in this video we're gonna continue building out our database app and create the ability to delete records from our database or in the last video we built out the graphical user interface we added the ability to add a record to view a record and I think that's all we got in this video we're gonna take a look at how to delete a record from our database but before we get started if you like this video I want to see more like it be sure to some hash to like button below subscribe to the channel and check out coding me comm where have dozens of courses with hundreds of videos that teach you to code use coupon code youtube to get $20 off membership that's all my courses videos and books for a one-time fee of just $27 which is insanely cheap ok so first things first let's all in case you didn't see the last video let's just run this real quick and show you what we have so far so here's our database we can add things in here click the Add button it adds it we can show them and we have John elder Bob Smith Dean and Miller we can you know configure this to output any of this stuff that we want but just to make it easier we've just put the first and last name I think right now let's go ahead and add the user ID number that oh I D number that we talked about earlier because we're gonna need that in a minute to delete records and I'll go into why that is so let's pull our code back up and go to the C query section here and down here where we're outputting the results on the screen we have first name which is this record 0 with item of the list I guess the tupple and then the first item is the last name I believe the OID is the 6th or the 5th so let's just go ahead and continue concatenating and let's add another space and then concatenate again and here we just want to copy all this in between these just paste this in and I'm not sure if it's the 5th or the 6 let's try the 6th and see so let's save this run it again real quick pull this over show records okay that was right so now we're getting this number next to these and that's cool that works let's see we can get a little crazy if we want and this might not work but let's add another plus and then inside of here we can put a backspace a backslash T that stands for tab if we want to tab that over a little bit so let's save that and give it a look now this may or may not work it'll definitely work but yeah okay because sometimes if these don't line up correctly the tabs go over a little bit too much or not enough so okay so we now we have the first names last names and the ID at the top of here we could put a little you know a thing that said first name last name or a little field that says name and then a little field that says ID number or something we'll just leave it like this for now um let's see I'm seeing this right up here this is bothering me it's shoved right up to the top let's push this down a little bit we can add some padding to this so we just want to do this first name in this text box here so real quick I'm just gonna scroll down to the text box section so here we have here we go text boxes so first name so here I'm just gonna add a pad Y and set that equal to now we haven't done this before you can add a tupple here if you only want to add padding to one side so I just want padding on the top so that's I'm gonna add 10 to the top and then I can go comma zero and I want to put no padding below so we'll do that and we could just copy this whole thing come down here to the label I want to do the same thing to the label that's right there there we go so let's save this and run it just to see what we have here just playing around at this point cuz it's fun okay that looks a little better it's push down a little bit and they still all want line up so okay now we want to talk about how to a delete record so if we click the show records we have these records right John elder Bob Smith and Tina Miller and we want to delete them now there's a couple of ways to delete you can say you can look for a specific record and then say delete this record but what we search for is important so if we said search for John elder and delete that record that will work but there may be four or five John elders in our databases common name right John's a common name elders fairly common you know Bob Smith there might be 50 Bob Smiths in your database so if you say delete Bob Smith that command will go through and delete every single Bob Smith in your database and you probably don't want that so we need to search by this Oh ID this primary key number this user number to delete things because each record in our database only has one specific unique ID number so if we say you know get rid of Tina Miller she's number three if we say delete number three we'll just lose Tina Miller which is what we want so how do we actually go about deleting things from a sequel ID database well it's pretty simple and we can come up here and let's just come somewhere up here to the top of our thing and let's go create function to delete a record all right so let's define our function and let's call it what delete probably good now inside of here we want to do all the same things that we've done in the past which is connect to our database and create a cursor so we could do that and then commit our changes and close so we can just paste these in here inside of here let's go delete a record so how do we do this well it's pretty simple it's just like everything we've done we use our cursor and we execute a command as we've always done and the command that we want is delete from and then name the table that we want to delete from and if you remember our table is addresses right and then now we want to use something called the where clause and this is just a sequel clause so delete from addresses where and here you designate the column that you want to search in so we want the OID column where the OID equals and then what what do we want well let's put placeholder here for now all right now I'm seeing this does not look right so we need to wrap all of this in quotation marks okay that's right delete from your database table addresses where oh I D equals placeholder now we could do the same thing we could go where f underscore name equals and then put quotes well we need single quotes John right we could do that but like I said that'll give us the problem where it'll go through and delete every single John in our database so we don't want that of course what we want is oh ID equals place holder now we'll create will change that placeholder in just a bit so what we need now is in our app we need a box where we can type in the number of the ID number that we want to delete and we need a button to actually delete let's go do that real quick and let's just come down here create text boxes underneath here let's go create well let's see we have some more down here yeah let's just go down right here and let's go create a delete button and just copy all of this and paste it in instead of query button let's call it delete button anything here delete button and this is not ro7 this is what we're on there was a row eight here so I think we're on row nine maybe let's try that we're oh nine column span is to a text pet why I have no idea what this is going to be but the command we want here is delete right because that's the function we created all the way up here just now this delete function right okay so let's change this to delete record all right so let's say this and just run it real quick to see if the formatting is correct it's probably not okay so this buttons a little bit bigger so I'll probably knock off what let's go 135 on the iPad X so save this run it again this is just for show basically that's pretty close let's add back one more so 136 save that run it okay that's pretty pretty good now we need probably what above this we need a little label and a box that says you know ID number or whatever so okay let's do that so I'm gonna add these think right here so let's go delete well let's start with the box itself right here let's go delete underscore box I don't know name it whatever you want and it's an entry box and it's in route and the width equals 30 like all the rest and then we can go delete underscore box dot grid and we want this in row let's go row equals nine column equals one okay I think that will do and then for a label for the same guy let's go delete underscore box underscore label equals label and that's in route and the text equals ID number I guess or we can do it out like that ID number yeah I work and again we want to now grid this onto the screen so delete underscore box label dot grid equals Rho equals 10 column type column equals 1 no zero right so now we have to let's put this back up here and we also have to change since we put these two rows above we need to change our button to row 11 okay I think that'll work let's save this and run it just to make sure everything looks okay and it does not everything's all sort of messed up oh we change it to be the same row obviously doing all right so this should be row 9 and then we need to change our button to row 10 all right it's Monday morning you got to bear with me Monday morning in Vegas all right that's better IDs sort of kind of weird so let's go back and change it to what do we want what do we have it before it was ID let's go delete ID so we're really explicit and what's going on here okay now we're good so now we want to be able to you know if we show the records that's showing up we're probably gonna want to change that to put it underneath it so let's do that right now all the fun with camp that would be the query and here the query labels so instead of row a we want row 11 now so let's save this run it make sure this is working oh guys a good stuff in this video alright so show records boom that pops down below there you might want to play with the padding here this is kind of close this there's more space in between these two then there is in between this so just you know you might want to do that I'll just leave that for now you can play around with the padding and this stuff if you like I just add a pad wide to these two well let's just do that right now why not we're doing all the things this morning alright so back down here to our delete and here we'll just go what had y equals let's gate add this v delete box label now where's the actual delete box right here had y equals give that a five save this run it again okay that's looking a little better I like that okay so now we need to fix our delete record button to actually delete a record so let's say well we'll do that later actually delete one later after we fix it let's clear the screen start to get a little crazy alright so when we click this delete button it calls the delete command or the delete function which is up here we did at the beginning of this video and we want to get rid of this placeholder and this is kind of a strange thing we don't want to what we're gonna do is come down here and get our let's see delete box entry and we're going to get that like we've done before but your instinct is going to be to go delete box get like that but that doesn't work what you have to actually do is concatenate that on afterwards so if we save this now come back here and run it and pull this over we can show it records Bob Smith let's delete number two if we show the records again some weird formatting here but it goes from one to three now if we close this and run it again and show our records we see number two Bob Smith is gone and it was just that easy so pretty simple delete from your tableware oh ID which is the primary key that we've talked about before equals and then concatenate this on here now this is a string now this is weird because it's an integer in the database so you would think it would need to be an integer but you can't concatenate an integer on here so for some reason with tkinter you can pass an integer like this as a string and it will still delete it even though the database has it as an integer so that's how you delete from a table not too bad and in the next video I think we'll learn how to update a record so we have these things here what if we misspelled Miller how do we change that I will look at how to do that in the next video so that's all for this video if you liked it be sure to smash the like button below subscribe to the channel and check out co2 be calm or you can use coupon code youtube to get twenty two dollars off memberships eight pages twenty seven dollars taxes all my courses hundreds of videos and the PDFs of all my best-selling coding books join over fifty thousand students learning to code just like you my name is jhanelle they're from coding me calm and i'll see you in the next video
Info
Channel: Codemy.com
Views: 32,677
Rating: 4.9661493 out of 5
Keywords: python, tkinter, gui, python tkinter, tkinter python, python gui, tkinter gui, gui python, graphical user interfaces python, tkinter grid system, grid system tkinter, python grid system, build graphical user interfaces with python, how to build graphical user interfaces with python, how to python tkinter, SQLite, SQLITE python, Python SQLite, SQLite Delete Record
Id: c9_gcIeAru0
Channel Id: undefined
Length: 16min 33sec (993 seconds)
Published: Mon May 13 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.