Page 53 - Phyton_GUIprogrammingwithTkinter
P. 53

P a ge  | 44


                        show_data(db)
                        customer_id = input("choose customer id> ")
                        name = input("New Name: ")
                        address = input("New Address: ")
                        sql = "UPDATE customers SET name=%s, address=%s WHERE
                 customer_id=%s"
                        val = (name, address, customer_id)
                        cursor.execute(sql, val)
                        db.commit()
                        print("{}                           data                         successfully
                 updated!".format(cursor.rowcount))

                 def delete_data(db):
                        cursor = db.cursor()
                        show_data(db)
                        customer_id = input("choose customer id > ")
                        sql = "DELETE FROM customers WHERE customer_id=%s"
                        val = (customer_id,)
                        cursor.execute(sql, val)
                        db.commit()
                        print("{}                           data                         successfully
                 deleted!".format(cursor.rowcount))

                 def search_data(db):
                        cursor = db.cursor()
                        keyword = input("Keyword: ")
                        sql = "SELECT * FROM customers WHERE name LIKE %s OR address
                 LIKE %s"
                        val = ("%{}%".format(keyword), "%{}%".format(keyword))
                        cursor.execute(sql, val)
                        results = cursor.fetchall()
                        if cursor.rowcount < 0:
                               print("No record")
                        else:
                               for data in results:
                                      print(data)
                 def show_menu(db):
                        print("=== KITTY TOYS SHOP APPLICATION ===")
                        print("1. Insert Data")
                        print("2. View Data")
                        print("3. Update Data")
                        print("4. Delete Data")
                        print("5. Search Data")
                        print("0. Exit")
                        print("------------------")
                        menu = input("Choose Menu> ")

                        #clear screen
                        os.system("clear")
                        if menu == "1":
                               insert_data(db)
                        elif menu == "2":
                               show_data(db)
                        elif menu == "3":
                               update_data(db)
                        elif menu == "4":
                               delete_data(db)
   48   49   50   51   52   53   54   55   56   57   58