Advertisement
Guest User

Untitled

a guest
Apr 6th, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.84 KB | None | 0 0
  1. import pymysql
  2.  
  3. connection = pymysql.connect(
  4.     host='localhost',
  5.     user='root',
  6.     password='',
  7.     db='buku',
  8. )
  9. def tampil():
  10.     try:
  11.         with connection.cursor() as cursor:
  12.             sql1 = "SELECT `id_buku`, `title`, `category`, `harga` FROM buku"
  13.             try:
  14.                 cursor.execute(sql1)
  15.                 result = cursor.fetchall()
  16.                 print("Id_buku\t Title\t\tCategory\tHarga")
  17.                 print("---------------------------------------------------------")
  18.                 for row in result:
  19.                     print(str(row[0]) + "\t" + row[1] + '\t' + row[2] + "\t\t" + str(row[3]))
  20.  
  21.             except pymysql.Error as e:
  22.                 print("Oops! Something wrong", e)
  23.  
  24.         connection.commit()
  25.     finally:
  26.         connection.close()
  27.  
  28. if __name__=='__main__':
  29.     tampil()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement