Page 96 - PYTHON-12
P. 96

Deleting record(s) from table using DELETE


                   import MySQLdb

                   db1 = MySQLdb.connect("localhost","root","","TESTDB" )


                   cursor = db1.cursor()

                   sal=int(input("Enter salary whose record to be deleted : "))

                   #Preparing SQL statement to delete records as per given condition


                   sql = "DELETE FROM EMP WHERE salary =sal”

                   try:

                        cursor.execute(sql)


                        print(cursor.rowcount, end=" record(s) deleted ")

                        db1.commit()


                   except:

                         db1.rollback()

                   db1.close()


                   Output:

                   >>> Enter salary whose record to be deleted: 80000

                   1 record(s) deleted


                   >>>
   91   92   93   94   95   96   97   98   99   100   101