Advertisement
afrizalwahyuadi66

mysql-delete.py

Dec 18th, 2024
26
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.17 KB | None | 0 0
  1. import mysql.connector
  2.  
  3. db = mysql.connector.connect(host="localhost",
  4.                              user="root",
  5.                              password="", database="db112300xx")
  6.  
  7. cursor = db.cursor()
  8.  
  9. while(True):
  10.     npm=input("Input NPM: ")
  11.     val=(npm,)
  12.     sql = "SELECT * FROM tblmahasiswa WHERE npm_mhs=%s"
  13.     cursor.execute(sql,val)
  14.     results = cursor.fetchall()
  15.     if(cursor.rowcount==0):
  16.         print("Record tidak ada/ditemukan...")
  17.     else:
  18.         for data in results:
  19.             print(f"Nomor Record           : {data[0]}\n"
  20.                   f"NPM Mahasiswa        : {data[1]}\n"
  21.                   f"Nama Mahasiswa        : {data[2]}\n"
  22.                   f"Jenis Kelamin (P/L) : {data[3]}\n"
  23.                   f"Program Studi (SI/IF): {data[4]}\n")
  24.         hapus=input("Hapus data? (y/t) ").lower()
  25.         if(hapus=="y"):
  26.             sql = "DELETE FROM tblmahasiswa WHERE npm_mhs=%s"
  27.             val = (npm,)
  28.             cursor.execute(sql, val)
  29.             db.commit()
  30.             print("{} data berhasil dihapus".format(cursor.rowcount))
  31.         ulang=input("\nUlangi hapus data? (y/t) ").lower()
  32.         if(ulang!="y"):
  33.             break
  34.  
  35.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement