Page 123 - PYTHON-12
P. 123

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


                   >>>
   118   119   120   121   122   123   124   125   126   127   128