Connect Mysql Database using Python Tutorial | MySQL server and Workbench 2024

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey everyone in this video I'll be showing you how  you can connect python with mySQL and run queries   using Code so I'll be showing you the entire thing  from how to download and install Python and MySQL   workbench and also how to connect both so let's  get started so just go to Google and search for   download python you'll be getting the first link  python.org just click that and here we'll be   having the latest version of python which is the  3.12.0 this is the upgraded version just click on   this and your download will be getting started so  it is about 25 MB in size so it gets downloaded   in a Flash just open it and you'll be getting the  python setup over here I want you to check both   of these options okay you need to also check this  add python.exe to the path if you don't do this   you'll be getting some errors when you run your  Python program so just check both these things and   just click on this install now just click on yes  and you'll be getting setup was successful just   click on close now just go to search and type for  environment variables you'll be getting the edit   the system environment variables just click on  open and here you need to click on environment   variables and under user variables here you'll  be having something called as path click that   and click on edit so you'll be having this two  python paths automatically added over here so if   you don't have these paths I would recommend you to  add these paths okay so now we are done just close   everything let's just go to command prompt and we  need to run the command python --version   so here you can see I'm getting python 3.12.0  which means that we have successfully configured   and in case if you want to run any Python program  immediately what you can do is you can just go to   search and type idle you'll be getting this idle  python 3.12 app just click on open and here you   need to go to file you need to click on new file  and this will give you an interface where you can   run any Python program so I'll just go with print  hello that's it I'll just and I'll just save this   you can save it anywhere something and for running  this just click on run and I'll be getting the   output over here so just go to Google and type  MySQL you'll be getting the first link as myql.com   just click this and in this website here you'll be  having this download option just click on that and   in the downloads you need to just scroll down  and here you'll be having this MySQL Community   GPL downloads just click that and here we will be  having so many MySQL things but we need to go for   MySQL installer for Windows all right so here also  we'll be having workbench please don't click click   that you need to click MySQL installer for Windows  select that and you'll be having this MySQL   installer 8.0.34 which is the latest version and  just select Microsoft Windows and if you scroll   down here you'll be having a web Community one  and you'll be having the installer community so   you need to install the MySQL installer community  and not the web one just click on the download and   you need to click on this no thanks just start  my download yeah so the download has begun it's   about 331 MB in size so just be patient till then  so once the download is complete just click on open so you'll be getting this MySQL installer  type thing and by default you'll be having this   server only option enabled all right so you need  to click on custom alright and you need to click   on on next and here we will be having the list  of products that are offered by MySQL so I'll   just click on this plus icon nearby MySQL servers  and here you'll be having this MySQL server click   on the plus again and click on the plus again  in MySQL server 8.0 and you need to just select   this thing okay MySQL server 8.0.3 4 it needs to  get highlighted in blue and you need to click on   this green green color Arrow so once you do this  you'll be getting that name over here which means   that this particular product or software is going  to get installed on your system alright this is   a standard thing don't ignore this okay next thing  we need to go into applications and here we will   be having the most important thing which is MySQL  workbench which is like a GUI tool for MySQL it's   super important all right same thing just click  the plus icon plus icon click that blue color   enabled and click the green color right arrow  you'll be getting there and I think MySQL shell   is also important same thing click that and green  color arrow and you also have MySQL router which   is not most commonly used I've never touch that  thing and you also have the documentation and   examples which are also not needed so I think  these three are pretty much the standard things   that everybody use so I'm good with this these  three things please install these three and just   click on next and you'll be getting the same more  here which is having the status ready to install   and once we click on execute here it says right  the installation of these packages will begin so   let me just now click on execute so here you can  see right it's installing and the progress appears   over here so once all the packages are installed  you'll be getting this green color Mark and you'll   be having the status as complete so just click  on next and ready to configure yeah this is fine   just click on next and here you'll be having the  port and the development computer so just don't   touch this stuff okay just click on next use  strong password encryption for authentication   yeah this is fine just click on next and here you  need to be super super careful okay you need to   enter your MySQL root password so I would really  recommend you to like save this password somewhere   it's really a hard process to reset this and it  may like require you to uninstall and reinstall   your MySQL setup so I would basically you know  have a familiar password that's the thing so   I'll just enter my familiar password over here  or you can just have it somewhere else so yeah   we can it says weak I don't care I will just  click on next and yeah standard account this   is fine service name everything is fine just  click on next yeah Grant full permission for   the user yeah this is also fine I'll just click  on next and these are the steps that are going   to take place now I just click on execute and our  installation will begin now after some time you'll   be getting the configuration for MySQL server  was successful click finish to continue I'll   just click on finish yeah I'll just click next  again so here you'll be having two options start   MySQL workbench after setup start MySQL shell  after setup I'll just click on finish and I just   enabled these two to make sure that they have  installed properly so I'll just click on finish so this is the MySQL shell over here and this  is MySQL workbench so first let us finish off   MySQL workbench then later we will go to mySQL  shell all right so first thing what you need to   do is you need to just go to services and you  to click on open and in the services you need   to have this thing MySQL 80 yeah so this needs  to be running and I would recommend you to set   it up as automatic so in the properties you need  to have this as startup type as automatic so if   you don't have it automatic you need to like  every time go here turn it on manually so I   would recommend you to have this as automatic  startup type so just select this and click on   automatic yeah so this is running now so this is  fine so now let's go to our MySQL workbench and   you need to click on this plus thing and here  you'll be having this connection name so I'll   just type my first connection something like  that and you to just click on okay that's it   you'll be having the connection over here just  double click it and here you need to enter the   password that you gave during your MySQL setup  so I'll just enter that password and I'll just   click on okay so this will take me in and here  I can run any MySQL Query MySQL so let me just   now first go and create a database and make  sure that the command is working fine so let   me just go and type create database I'll just  go with the name new name this is going to be   the name of my database something I'll just run  it so I'm getting here a green Mark which means   that this query has been executed successfully  so I'll just go with something show databases   yeah this is the command to display the list  of databases so we will be getting these things   and I know what you thinking the font size is  very small so to increase the font size what   you can do is you need to go to edit and you'll  be having the preferences and inside preferences   you'll be having this fonts and colors the next  one to modeling just click fonts and colors and   basically just you know change everything to  like 30 or 40 yeah just change it because I   think the size of the fonts throughout MySQL is  small so you know better change everything to a   decent number and just click on okay yeah and  it won't get changed so let me just now close   my connection and if I just now again go into  my connection just enter it so now you can see   the font size has drastically improved a lot so  this is good now we done with our MySQL workbench   setup so now let's go and do the MySQL shell  setup so if I just now go to my command prompt   and if I just now type MySQL hyphen iph version  I'll be getting this MySQL is not recognized okay   so for that let's fix it now so let me just now  close this and you need to go to the folder where   you have installed MySQL all right so by default  it's going to be C program files and here you'll   be having MySQL and inside that you need to go to  mySQL server 8.0 it's not shell it's not workbench   it's server and inside that you'll be having  this bin click that and you need to basically   just copy this thing copy it fully all right and  in case if you it's hard for you to to find this   path what you can do is you can basically just go  and search for MySQL you'll be getting this MySQL   workbench just click on open file location right  click it again open file location just go back   your folder you'll be having this MySQL server  8.0 and inside this bin we will be back to the   normal path where we are began just copy this path  till bin under server 8.0 after copying it go to   search and type for environment you'll be getting  this option called as edit the system environment   variables just click on open and in this you'll  be having this environment variables thing click   that and under system variables just click on  path and you need to click on edit and in this   you will be having an option called as new click  that and you to just paste this path be careful   it's MySQL server 8.0 and till bin just click  on okay okay everywhere here also and in case   if you are having your command prompt open just  close and reopen it again otherwise you won't be   getting the changes affected so I'll just go  again to search CMD I'll just zoom it and if   I type MySQL hyphen iph version so here you can  see just a moment ago I was getting some kind of   error but now I'm getting the version which  is the latest version that we have installed   right now so after this in order for us to run my  SQL commands through our terminal what you can do   is you to type MySQL hyphen u space root hyphen  P just hit enter and here you need to enter the   password that you gave installed MySQL server  and workbench so that's the reason why I told   you that password is like super important without  that password you cannot do anything here so just   enter that password and here you can see now I'm  getting this this MySQL command prompt thing like   a shell where I can run MySQL queries so I'll just  go with show data bases just hit enter so here you   can see that I'm having this new name database  which is the database that I created a moment   ago so as I told you this workbench is super  useful tool just a GUI basically you know   shows the names and rows present in a column in  a much more Visual and appealing way so so yeah   both have the same data at the end of the day  so now we are done setting up both Python and   our MySQL workbench database so now let's just go  and write the code for connecting our python code   with this database over here so for this just  go to search and type idle and if you have any   other text editor you can use that I will just  use the default python 1 open it and here you'll   be having at the top called as file click that  and just select new file so you can just save it   I'll just click on save as and you can just type  anything just save this first now we are good   and in case if you want to change the font style  just click on options configure idle here you can   change the font size and just click on apply and  okay yeah so this is fine so now let's just begin   our coding so before that we need to install one  particular python package okay just go to Google   and type pip MySQL python connector you'll be  getting this first one this is going to be pip   install MySQL hyphen connector hyphen python just  copy this or else you can just see the video and   type this all right and now go to search and open  command prompt and just paste this command and hit enter that's it this particular package will be  getting installed that's it so now again go to   our python file and we need to import this so  it's going to be import MySQL do connector so   this is going to be the only package that we'll  be importing and and next is going to be the   connection variable with this particular statement  we will be able to directly connect with our MySQL   workbench so it's going to be CON Let It Be The  Connection variable equal to mySQL do connector   dot connect and within circular bracket we  need to specify some information over here   first is the user the user is going to be in my  case I have mentioned the username as root if   you remember my MySQL workbench I have given my  connection username as root so if you have given   something else you need to give that username by  default it's going to be root so that's good over   here root and next thing is going to be the host  here we are just using Local Host so it's going   to be host equal to within single quote just  type Local Host comma next is going to be the   database name so it's going to be database equal  to and here we need to specify the database that   we just created now so in our case it's going to  be this new name I'll just copy this go here and   paste it within single codes next one is going  to be the password so for password we are not   writing the full keyword so it's going to be p  w so this is going to be the password and just   enter the password for your MySQL account okay  so I will enter mine so now we are good with the   connection variable so let's just go and print  this now so for just making sure that we are not   getting any errors let's just print this variable  so I'm saving it and for running this it's going   to be run and click on run module so here you can  see we are not getting any errors so now what we   can do is let's just go and create a table inside  this database named new name so I already have   some tables inside this database so if I just  you know go inside tables I'm having these many   tables already created so I'll just create a new  table inside this datab datase with the help of   python code all right so for doing that what you  need to do is just come to the next line we need   to create something called as cursor and only  with the help of this cursor object we will be   able to directly execute MySQL queries in Python  so I will create that cursor variable so let me   name it as Cur equal to our connection variable  name dot we will be using the cursor function   so it's going to be cursor function that's it  so now we can write our MySQL queries directly   inside it so it's going to be something like  this our cursor variable name dot execute and   within circular brackets and inside that we need  to have double quotes and inside this we will be   specifying our normal MySQL queries so I'm having  here a simple query to create a table so this is   just a create table customers I'm just going to  create this table now so I'll just copy and paste   it inside this so this just a create table in SQL  create table this is my table name and this is   going to be my two columns one is column named  name another column name is ID and one column   is going to have the data type as Vare or string  and another column is going to have the data type   as integer so this is going to be my thing and  final thing is going to be the commit function   so for MySQL to replicate and reflect all these  changes we need to add this final commit function   so for that it's going to be our connection name  and be careful over here this is our connection   variable name and not our cursor variable name  okay so this is the first variable that we have   used it's going to be dot commit and a function  that's it so now let me just save it and here you   can see if this code runs perfectly we will be  having here a table named customers because we   don't have that particular table at the moment  right and if this code is a success we will be   getting that in the output so I'll just click on  run run module so we won't be getting anything   over here but if I go to my MySQL workbench and  if I just now refresh this here you can see I'm   having a table called as customers and if I just  go into the information of the table and if I go   to columns here you can see I'm having two columns  named ID and name type int and VarCHAR which are the   same thing that we have specified over here which  means that we have successfully created a table by   using our python code in MySQL so that's the  video guys I hope you would have found this video   useful do check the playlist of my channel I have done  a lot of programming videos in Python C C++ Java   and lot of MySQL queries and AI videos also  check them out subscribe me thanks for watching
Info
Channel: United Top Tech
Views: 535
Rating: undefined out of 5
Keywords: how to connect mysql database in python, connect python to mysql, mysql python connector install, python mysql tutorial, Connect mysql database using python, Python, MySQL Connector, Python with database, mysql, python database connection, python database connectivity, how to connect mysql database with python, python database connection mysql, how to connect python with mysql
Id: sdZMlv1ZSK8
Channel Id: undefined
Length: 19min 49sec (1189 seconds)
Published: Sat Dec 23 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.