HOW TO CONNECT PYTHON and MYSQL (Database Connection Program)

Connectivity
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. 


mysql.connector.connect()
·     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.
conn.is_connected()
·   it is a function of the MySQLConnection class through which we can verify is our python application connected to MySQL.



connection.cursor()
          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.
 connection.close()
At last,  closing the MySQL database connection usea close()   of MySQLConnection class.














for python full source code click here
Previous Post Next Post