PYTHON DATABASE CONNECTIVITY || HOW TO CONNECT MYSQL WITH PYTHON || CONNECTING DATABASE WITH PYTHON

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hello friends welcome back to our channel so in the previous session we have seen how to download and install the mysql and now in this section we will see how to establish the connection between the python application and the database so database connection so here there is a lot of databases so sql mysql sql server so all these are different databases now i will show how to establish that connection between the python application and mysql because in the previous session we have seen the installation of mysql now we will see the connection how to create a connection so for the connection we require four steps in order to connect to the database we require four steps the first one is import a module called mysql dot connector mysql dot connector so first we need to import this module and after that we have to establish the connection so establish connection using connect method connect method right and this connect method will be called with the help of module name so my sorry mysql dot connector dot connect and here we need to go with the four parameters the first one is a host hostname okay so host is equal to name the first one the second one is a user so in which user we are giving we are accessing username and password so these are the parameters you just remember this fillings right so we have to go with the password by with pass sswd is equal to the password password and the fourth parameter is a database so a database can have a multiple databases right so in which database we need to access the data and all these things should be given here itself so here the db name the database name okay so this is this is the syntax in order to establish the connection between the mysql application mysql and the python application right so this is the second step so by using the connect method using four parameters we need to establish the connection okay so give that object okay give one object so connection object is equal to mysql dot connector dot connect and give this one okay this is the second step the third step create cursor object so in order to execute the query that query should be written with the help of this cursor object okay so here the object name is equal to connection object so whatever the connection object we have given dot cursor method right so that here this is the cursor object so this is the third step so this is the first second third and finally fourth one execute the query so in order to access the data from the database we need to write the claim so in order to create a table we are having some query syntax and in order to delete the content from the table we are having another query in order to alter the table we are having some query so for everything we are having a different query right so the last step is executing that query with the help of cursor object with the help of cursor object and this will be done this will be done with the help of see i will write here cursor object name so by using cursor object name dot execute method so here we need to write some execute method and here we need to write the query we need to write the query in double quotations so these are the four steps so this is the fourth one right so in order to create the database also we have to write a query so that query should be written here itself right so first step importing the module second step establish the connection using the connect method and third one create the cursor object so by using the cursor method and then fourth one execute the query with the cursor object right so hope you understood these are the four steps to create a database connection or to access the data from the as mysql to the python application right so in this session i will show you how to connect this mysql with the python application and also how to create a database so a new database okay inside the mysql there will be lot of databases right so how to create a new database and from the next session we are going to create a table in that particular database itself right so i will show you the queries from the next session so in order to create the table and accessing the data from the table right so let us move on to the computer so that i will demonstrate how to connect uh to the database from the python application let's move on hello friends so just now we have seen how to establish the connection between the mysql database with the python application so here i have chosen mysql as a database in order to connect with the python application so we'll execute the code i will demonstrate by executing some program so we have seen the four steps to be done so first one we need to import the package so import the library function is mysql dot connector so first we need to import this one after that we need to establish a connection connection with the help of connect method so we need to execute the connect method with the help of that module name so i will go with c o n n is equal to mysql dot connector dot connect so in this connect method there will be four parameters first one is a host name we need to give the host name so here the host name is a localhost and next you need to give the user and the password so in which user you need to create the database in in order to access the data so the first user here i am using the root as a user and then password so here i have given a password as a computer so and the fourth one is a database in which database we need to create a tables first of all let us create one databases a separate database and in that database we will create some tables so that's why i am not giving the fourth parameter that is a database now if you print this one you can find the connection so this is the connection between my uh the database as well as the python application now the next one is in order to execute the queries so everything will be retrieved with the help of queries and if you want to execute that query we need to create one object in python so that is a cursor object so we'll create some c is equal to connection dot cursor right so cursor and here you can observe if you print c will get the cursor object so nothing executed yet because we we have not executed any query we have just created one cursor object now in order to execute a query there is a function called execute so c dot execute execute and here we need to write the query as a parameter to the execute function first let us know what are the databases available in the mysql and then we will create one more database so in order to know the databases the query is show databases show databases and here in order to know the databases you just go with the cursor object for inc print i so that you will get all the cursor objects so you can observe here so just remove this print and execute this one see these are the for available databases these are the available databases in mysql you can also go with the command line mysql command line so just give the password here and you just give the command so databases semicolon so you can find so these are the databases available you can observe the same here okay so a connection has been established and started accessing the data right now our next step is we'll just create a database we just create a database so these are the four steps first import establish the connection create the cursor object and execute the query now i just want to execute the query you can change this one so remaining all are same so i will create a database so create database followed by database name okay so here we can give we can give pydb or uh we can go with a sardi okay right so our database name is saruti right so you can observe sardi is not available here information schema mysql performance underscore python db sandeep says test one and word so these are the databases available and now i am creating some sardine right so let us create this one so it was created now you can check here okay in the mysql command line client you can check here once again go with the show databases so you can find the things are there so previously there is no sardi database right okay so like that we can create a database and now in the database we can create the tables so also we can go with the mysql bench and i will check it out right so click on this mysql bench so after installing mysql we can access the mysql by using mysql command line client or mysql workbench so now i will show you in the mysql workbench that is a graphical user interface so click on this local instance it will prompt for the password i will give the password and click ok so it will be connected and you can find here see these are the databases on the left my left menu you can observe the databases python db circulars are these and deep sis test one and world these are all the databases and you can this is a command prompt and you can work our work here so show databases so execute this is the execution so if you execute this one you'll get all the databases right so you can access the data database or a data or a table by using either command line client or a mysql workbench right so this is how we can create the database so in this session we have seen how to establish a connection between the python application and mysql and after that how to create the cursor object and how to execute a queries so everything will be common and we will just change the query so whatever the query we are using will change the query so in order to create a table we have to write the query for creating a table here itself and in order to insert the value we have to write the query here itself so the query will be keep on changing but the four four steps will be common for every program right so whatever the thing we need to do either either you want to create a table either you want to alter a table you either you want to drop a table you want to insert the rows whatever the thing you need these four steps are common first one we have to import the module and then we need to establish a connection after establishing a connection we need to create a object so that it will be executing multiple queries so in order to execute the query we are using some execute function right so let's stop here so if you are having any doubts regarding this connection connectivity uh feel free to post your doubts in the comment section uh definitely i will try to clarify all your doubts and if you real understood by session like my session share my session with your friends and don't forget to subscribe to our channel so thanks for watching thank you very much
Info
Channel: Sundeep Saradhi Kanthety
Views: 13,509
Rating: 4.9589043 out of 5
Keywords: SUNDEEP, SARADHI, KANTHETY, PYTHON, PROGRAMMING, FEATURES, APPLICATIONS, INTERPRETER, PYTHON SHELL, INTRODUCTION, BEGINNERS, PLATFORM INDEPENDENT, PYTHON BASICS, PYTHON FUNDAMENTALS, LIST, SET, TUPLES, DICTIONARY, FUNCTIONS, LIBRARIES, conditional statements, applications, types of arguments, complete python, mysql, python database connectivity, connecting database, mysql connection, mysql connector, connect methods, execute query, show databases, mysql database, database creation
Id: 1mxzeiHlTK8
Channel Id: undefined
Length: 14min 37sec (877 seconds)
Published: Fri Jan 29 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.