Advertisement
afrizalwahyuadi66

mysql-select.py

Dec 11th, 2024
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.89 KB | None | 0 0
  1. import mysql.connector
  2. db = mysql.connector.connect(host="localhost",
  3.                              user="root",
  4.                              password="", database="db11230070")
  5.  
  6. cursor = db.cursor()
  7. sql = "select * from tblmahasiswa order by id"
  8. cursor.execute(sql)
  9. results = cursor.fetchall()
  10.  
  11. if(cursor.rowcount==0):
  12.     print("Record tidak ada/ditemukan...")
  13. else:
  14.     idx = 1
  15.     for data in results:
  16.         print(f"Nomor Record        : {data[0]}\n"
  17.               f"NPM Mahasiswa       : {data[1]}\n"
  18.               f"Nama Mahasiswa      : {data[2]}\n"
  19.               f"Jenis Kelamin (P/L) : {data[3]}\n"
  20.               f"Program Studi (SI/IF)   : {data[4]}\n")
  21.         if(idx==5):
  22.             idx = 0
  23.             ulang = input('Tampilkan record selanjutnya ? (y/t) ').lower()
  24.             if(ulang!='y'):
  25.                break
  26.         idx+=1
  27.    
  28.     print(f"Jumlah Record : {cursor.rowcount}\n")
  29.    
  30.  
Tags: mysql-python
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement