Using Databases With TKinter - Python Tkinter GUI Tutorial #19

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what's going on guys John elder here from coding be calm and in this video I'm going to show you how to start using databases with kinter and Python or in the last video we looked at drop-down menus in this video I want to start to talk about how to use databases with kinter and Python and this is likely to take several videos pretty big topic and we're gonna be using the sequel Lite database for this and I'll talk about that in just a second but before we get started if you like this video want to see more like it be sure to smash the like button below subscribe to the channel and check out coda me comm where have dozens of courses with hundreds of videos that teach you to code use coupon code youtube to get $22 off membership that's all my courses videos and books for a one-time fee just $27 which is insanely cheap ok databases up until now we've been having some fun with Kenter but adding a database to any type of program vastly improves the sort of power of that program you can do a lot of stuff with databases we just can't do without so you know likely whatever you build ever you're gonna need a database so we're gonna start looking at how to use a database with kinter so we're NEAs a sequel 8-3 database which comes with python so it's super easy to use most people don't even realize it comes with Python and it's not very powerful it's not like my sequel or Postgres but it's great for small projects test projects hobby projects things like that and if you learn how to use it it's really easy to learn how to use my sequel and Postgres after that so it'll be easy to sort of switch over if you need something more powerful so to use sequel Lite with our kinter program all we have to do is import it and it's almost ridiculous how easy it is to to import this all we have to do is type in import sequel Lite 3 and boom we now have sequel Lite database in our program very cool all we have to do now is connect to it and start using it so I've created a new file called database PI I have the same basic starter code that we've been using forever so the first thing we need to do is create a database or connect to one that already exists and the commands for both are the same so let's go create a database or connect to one and to do that we create a connection to the database and so we just create a variable call it anything you want but I'm going to call it conn short for connection and we're going to set it equal to c qualité 3 dot connect and then inside of here we just pass in the name of the database we want to connect to or create so I think in the next few videos we're gonna create a basic sort of address book app right so let's create a database called address underscore book that DB so this doesn't exist it exists yet right we haven't created it yet oops but like I said if it doesn't already exist this command will create it for us and it'll actually save it in whatever directory we're currently in so we've been saving all of our code in these videos in the GUI directory so it'll save address underscore book DB in the GUI directory super easy super cool so the next thing we need to do is create a cursor and a cursor is sort of like the little thing you send off to do stuff with the database so anytime we want to execute any sort of command the cursor does that we send it off and it does it comes back with the result that's the cursor and same thing we just create a variable I'm gonna call C for cursor you tend to type it a lot so I don't want to type out cursor every time so just call it C easy to type for all of you lazy people that are just as lazy as me so cursor okay so this is a cursor instance I guess s LS co-create cursor okay and anytime we make a change to our database we want to commit those changes to the database that's just a common database thing so to do that we just go Khan dot commit so let's go commit changes and then finally whenever we're done we always want to close our connection and we don't really have to whenever the program ends our connection closes automatically but it's sort of just the polite thing to do to explicitly close your database connection so we just go Khan dot close and that is that okay so we've got our cursor we've got our database created now we need a table if you know anything about databases you know a database isn't really anything it's the tables inside that do all the work hold all the data and it's the table that we're always interacting with and I think of a table as a spreadsheet it has columns and rows and we just need to designate what those columns are and then every time we add an entry that becomes a new row right so think like first-name lastname address zip code those are all columns in the database table so we need to create that table and designate those columns and so that's what we're gonna do now let's go create a table okay so to do that we use our cursor we always use our cursor and usually when we do stuff to the database we're executing some sort of command so we always almost always want to go execute here and then inside of here we need to you know do some sequel structured query language sequel commands to do whatever we want to do and in this case we want to create a table now usually you just put quotation marks and then you type in your commands but since we're creating a table tables are kind of big there's gonna be a lot of stuff for us to type here I'm gonna use these doc type string things and it's just six quotation marks is to open and a closed quotation marks and like I said this allows us to do stuff on multiple lines otherwise you could use single quotes and do it all in one line so what we want to do is create a table right and now we need to name it so let's call our table addresses right now inside of these parentheses we need to just designate the different columns that we want so let's go we want a first name and now we need to designate the data type and so this is gonna be text now the cool thing about sequel Lite is it only has five data types and those are text which is just text integers which are whole numbers 10 15 108 real which are decimal numbers 1995 2795 that sort of thing null which means does it exist or does it not exist and blob and blobs are like image files video files things like that so pretty simple databases usually have a lot more data types and it's always kind of complicated but you only ever use a few write text in numbers basically so that's one of the cool things about sequel I so we we have first name we want last underscore name as well and that'll be text oh we want the address and that's text and you'll notice I'm separating each one with a comma and that's pretty simple address let's go city that's a text let's go state that's text and let's go zip code and that's gonna be an integer okay so it's the last one we don't put a comma at the end of that I just sort of do this to make this look good and boom there we go now we could have used single quotes and put all of those on one long line but it's really hard to read this is much easier to read so that's why we use those triple quotation marks okay so I misspelled execute it looks like execute to E and that was hard execute there we go okay so we only need to run this once we need to create our table one time we need to commit those changes close our connections so I'm just gonna save this head over to our terminal here and we want Python database pi if we run it this box pops up there's nothing in here because we haven't done anything yet so we can just go ahead and close it and now if I pull up a little thing here and go into my C Drive and look at GUI you see now this address book that DB database file exists it created for us inside of it presumably it has the table that we just created as well we won't really know till the next video when we start putting data in and see whether or not it worked to find out if it did work but I think it worked that's pretty simple code right there so in the next video we'll start to build out our kinter app with fields and stuff so we can type in people and their addresses to add to the database and that should be cool I should mention very quickly on my website I've got an entire course on sequel Lite with Python so if you really want to get into the nitty-gritty of this check that out it's $29 for the course but of course if you sign up for total membership using that coupon code youtube' you get all my courses for just $27 in which case you'll get this one for free and it has it's an hour and a half long 22 videos and it has all the stuff in in much greater detail we're not going to go into great detail in these videos right here I'm just going to show you some basic stuff and you can learn more later if you want but definitely worth checking out if you're interested in all this database stuff 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 Kota Meachem or you can use coupon code youtube at $22 long membership you pages $27 to access all my courses hundreds of videos and the PDFs of all my best-selling coding books join over 50,000 students learning to code my name is John elder from Cody Beacom see in the next video
Info
Channel: Codemy.com
Views: 147,569
Rating: undefined out of 5
Keywords: python, python programming, python3, python 2019, learn python, learn python programming, learn to code, code python, learn to program python, python programming tutorial, learn python programming tutorial, python sqlite, python sqlite3, sqlite python, sqlite3 python, sqlite tkinter, tkinter sqlite, tkinter sqlite python, tkinter sqlite3, using sqlite with tkinter, using databases with tkinter, tkinter and databases, how to use a database with tkinter
Id: YR3h2CY21-U
Channel Id: undefined
Length: 9min 40sec (580 seconds)
Published: Tue May 07 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.