Page 52 - Phyton_GUIprogrammingwithTkinter
P. 52
P a ge | 43
Do-It-Yourself
1 - CREATE COMMAND LINE INTERFACE APPLICATION (CLI) BASED ON CRUDS
APPLICATION
You’ve have been asked to design a CLI application for Kitty Toys Shop which helps
management team of a shop to collect and keep a record of all purpose of their customers.
To do that, you will need to create a database and apply CRUDS (C reate, R ead, U pdate, D
elete, and S earch) to allow management team to manage the customer information.
By using MySQL connector, construct a Python program to create a database name ‘Toys_DB’.
Then write a program that can accept input from user which is customer name and customer
address. This record must be saved in the table name ‘CUSTOMERS’.
Program Code
import mysql.connector
import os
db = mysql.connector.connect(
host="localhost",
user="root",
password=""
database="Toys_DB"
)
def insert_data(db):
name = input("Enter your name: ")
address = input("Enter your address: ")
val = (name, address)
cursor = db.cursor()
sql = "INSERT INTO customers (name, address) VALUES (%s, %s)"
cursor.execute(sql, val)
db.commit()
print("{} data successufully saved!".format(cursor.rowcount))
def show_data(db):
cursor = db.cursor()
sql = "SELECT * FROM customers"
cursor.execute(sql)
results = cursor.fetchall()
if cursor.rowcount < 0:
print("No record")
else:
for data in results:
print(data)
def update_data(db):
cursor = db.cursor()