DELETE and UPDATE Command in Python PDBC

Master Python with 70+ Hands-on Projects and Get Job-ready - Learn Python

Program 1

# Program for PDBC Delete Data by id
import MySQLdb
try:
    con=MySQLdb.connect(host='localhost',user='root',password='root',database='DataFlair')   
    print("Data Base connected")
    empid=int(input("Enter employee id for delete: "))

    sql="delete from employee where eid={}"
    sql=sql.format(empid)
    cur=con.cursor()
    n=0
    n=cur.execute(sql)
    con.commit()
    if(n==0):
        print("No record found ")
    else:    
        print("Record deleted: ",n)
except Exception as obj:
    print(obj)    
finally:
    con.close()

Program 2

# Program for PDBC Delete Data by id
import MySQLdb
try:
    con=MySQLdb.connect(host='localhost',user='root',password='root',database='DataFlair')   
    print("Data Base connected")
    eno=int(input("Enter Employee No for update:"))
    name=input("Enter Employee Name:")
    mobile=int(input("Enter Mobile No:"))
    age=int(input("Enter Employee age:"))
    sql="update employee set ename='{}',mobile={},age={} where eid={}"
    sql=sql.format(name,mobile,age,eno)
    cur=con.cursor()
    n=cur.execute(sql)
    con.commit()
    print("Record updated: ",n)
except Exception as obj:
    print(obj)    
finally:
    con.close()

 

Your 15 seconds will encourage us to work even harder
Please share your happy experience on Google

courses

DataFlair Team

DataFlair Team provides high-impact content on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. We make complex concepts easy to grasp, helping learners of all levels succeed in their tech careers.

Leave a Reply