HOW TO CONNECT PYTHON and MYSQL (Database Connection Program)

Employee Management System

EMPLOYEE DATA MANAGEMENT” is a short but just example of how various functions like:-
  • ·        Showing details of all employees
  • ·        Add , no. of employees and details
  • ·        modify details of all or selected employees.
  • ·        remove all or selected items from the table.

import mysql.connector
·         This line imports the MySQL Connector Python module in your programto connect MySQL.

from mysql.connector import Error 
  mysql connector Error object  used to show  an error when we failed to connect Databases or if any other database error occurred while working with the database. 

·     function connect the MySQL Database, this function accepts four required parameters: Host, Database, User and Password that we already discussed.
·         connect() function established a connection to the  MySQL database from python application and returned a MySQLConnection object.  Then we can use MySQLConnection object to perform various operation on the MySQL Database.
·         Connect ()  function can throw an exception, i.e. Database error if one of the required parameters are wrong. For example, if you provide a database name that is not present in MySQL, then Python application throws an exception.
So check the arguments that you are passing to this function.
·   it is a function of the MySQLConnection class through which we can verify is our python application connected to MySQL.

          returns a cursor object. Using a cursor object, we can execute SQL queries.

  • cursor.close()

        cursor’s close method we  close the cursor object. when we close cursor, we can not execute any SQL statement.
At last,  closing the MySQL database connection usea close()   of MySQLConnection class.

for python full source code click here

About the Author

Sarkun is a dedicated research student at one of India's premier institutions, the Indian Institute of Science Education and Research (IISER). With over three years of experience in the realm of blogging, Sarkun's passion lies at the interse…

Post a Comment

Cookie Consent
We serve cookies on this site to analyze traffic, remember your preferences, and optimize your experience.
AdBlock Detected!
We have detected that you are using adblocking plugin in your browser.
The revenue we earn by the advertisements is used to manage this website, we request you to whitelist our website in your adblocking plugin.