Advertisement
afrizalwahyuadi66

mysql-update.py

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