Python- Update Operation

The UPDATE-SET statement is used to update any column inside the table. The following SQL query is used to update a column.

  1. >  update Employee set name = 'alex' where id = 110  

Consider the following example.

Example

  1. import mysql.connector  
  2.   
  3. #Create the connection object   
  4. myconn = mysql.connector.connect(host = "localhost", user = "root",passwd = "google",database = "PythonDB")  
  5.   
  6. #creating the cursor object  
  7. cur = myconn.cursor()  
  8.   
  9. try:  
  10.     #updating the name of the employee whose id is 110  
  11.     cur.execute("update Employee set name = 'alex' where id = 110")  
  12.     myconn.commit()  
  13. except:  
  14.       
  15.     myconn.rollback()  
  16.   
  17. myconn.close()  

Update Operation
Python Update Operation


Delete Operation

The DELETE FROM statement is used to delete a specific record from the table. Here, we must impose a condition using WHERE clause otherwise all the records from the table will be removed.

The following SQL query is used to delete the employee detail whose id is 110 from the table.

  1. >  delete from Employee where id = 110  

Consider the following example.

Example

  1. import mysql.connector  
  2.   
  3. #Create the connection object   
  4. myconn = mysql.connector.connect(host = "localhost", user = "root",passwd = "google",database = "PythonDB")  
  5.   
  6. #creating the cursor object  
  7. cur = myconn.cursor()  
  8.   
  9. try:  
  10.     #Deleting the employee details whose id is 110  
  11.     cur.execute("delete from Employee where id = 110")  
  12.     myconn.commit()  
  13. except:  
  14.       
  15.     myconn.rollback()  
  16.   
  17. myconn.close()  


;