Learn Python – Update Operation- Basic and advance

The UPDATE-SET statement is used to replace any column internal the table. The following SQL question is used to replace a column.

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

Consider the following example.

Example

import mysql.connector  
  
#Create the connection object   
myconn = mysql.connector.connect(host = "localhost", user = "root",passwd = "google",database = "PythonDB")  
  
#creating the cursor object  
cur = myconn.cursor()  
  
try:  
    #updating the name of the employee whose id is 110  
    cur.execute("update Employee set name = 'alex' where id = 110")  
    myconn.commit()  
except:  
      
    myconn.rollback()  
  
myconn.close()  

Delete Operation

The DELETE FROM announcement is used to delete a specific record from the table. Here, we have to impose a circumstance using WHERE clause otherwise all the information from the table will be removed.

The following SQL query is used to delete the worker detail whose identity is one hundred ten from the table.

>  delete from Employee where id = 110  

Consider the following example.

Example

import mysql.connector  
  
#Create the connection object   
myconn = mysql.connector.connect(host = "localhost", user = "root",passwd = "google",database = "PythonDB")  
  
#creating the cursor object  
cur = myconn.cursor()  
  
try:  
    #Deleting the employee details whose id is 110  
    cur.execute("delete from Employee where id = 110")  
    myconn.commit()  
except:  
      
    myconn.rollback()  
  
myconn.close()