Document
Document
In this article, we are going to make a simple project on a boutique management system using Python
MySql connectivity.
Introduction
This is a boutique management system made using MySQL connectivity with Python. It uses a MySQL
database to store data in the form of tables and to maintain a proper record of all details. Different SQL
queries that include DDL, DQL, DML, and DCL commands are used in this project which is useful to
perform CRUD (Create, Read, Update, Delete) operations using MySQL. This management system
provides a common platform for customers, employees, as well employers to use the services. The
various functionalities provided in this system are:
Customers: Customer can create their accounts, sign in to their accounts and perform various operations
such as book their orders, view booking, delete bookings and update their details.
Employees: Employees can sign in to their account using their employee id and perform operations such
as updating delivered orders of customers, adding a new product, and deleting a product.
Employer: The employer can perform two operations which are viewing product details and adding a
new employee.
Prerequisites
MySQL should be installed on our system. Refer to this link to Download the MySQL installer. Once
MySQL Server is installed properly, we can create databases and tables using MySQL commands in
MySQL Command-Line-Client. MySQL Command Line Client is installed together with the MySQL Server,
so you don’t need to download and install the MySQL client separately. To check whether you have the
client program on your machine, go to search, and type “MySQL”. To access MySQL Server from the
command-line client, open the program and enter the password after that, you will be able to use the
client.
The tables used for this Boutique Management System are made using MySQL Command-Line-Client.
Note: Refer to this article to know more about how to create databases and tables.
Creating database
USE sboutique;
The customer table has 6 columns, which store customer ID, customer name, last name, customer’s
phone number, address of the customer, and products booked.
c_nam varchar(30),
c_lnam varchar(30),
c_phno varchar(10),
c_adrs varchar(50),
bkd_pro varchar(40) );
Here cust_id is made the primary key because each customer is considered to be unique.
The employee table has 5 columns which stores, employee ID, employee name, last name, phone
number, and address.
e_lnam varchar(30),
e_phno varchar(10),
e_adrs varchar(30),);
Here employee ID is made the primary key because each employee is considered to be unique.
The products table has 4 columns with columns product number, product id, product price, and product
stock.
pro_num char(5),
pro_price int(6),
pro_stk int(5) );
Here product ID is made as the primary key because each product in the database has a unique ID.
('1','KWPTP25',330,18),
('2','KWPTP30',450,30),
('3','KWPTP45',650,20),
('4','SSST025',850,10),
('5','SSST030',350,12);
USE sboutique;
SHOW TABLES;
DESC customer;
DESC employee;
DESC products;
Now the tables are created in the database. This database can be used to store create records, update
values in tables, and delete records.
Getting Started
We need to make a connection between the MySQL database and Python so that the tables can be
accessed using python IDE. For making a connection we need to install mysql-connector which can be
done by writing the following command in the command prompt on Windows.
After installation of the mysql-connector, we can connect MySQL with Python which can be done by
writing the following code.
import mysql.connector
mycon = mysql.connector.connect(
host='localhost', user='root',
password='password',
database='sboutique')
mycur = mycon.cursor()
In the above code, we have to enter our localhost address, port number, and password that we have set
while installing MySQL workbench It will look like the below image after setup MySQL workbench also
we have to enter the database name on which we worked on.
The name of the MySQL Database used is “sboutique” and the following tables are used :
1. Check() Function
This function is being used to return a list of IDs of all the customers in the boutique. At the time of a
customer login or new customer, it is used to check if the customer with this ID already exists or not.
def check():
d = mycur.fetchall()
list_of_ids = []
for ids in d:
list_of_ids.append(ids[0])
return list_of_ids
The customer account function asks customers to enter their customer ID and it checks if a customer
with this ID already exists or not because customer ID is a primary key in the customer table in the
database and the values entered in these columns must be unique. If the customer exists it displays a
message, else it takes the customer’s details and inserts the record in the customer table of the
database.
ask = 'Y'
list_of_ids = check()
if custid in list_of_ids:
else:
c_det = ()
''' Values inserted in the table and default NULL value are
mycur.execute(qry, val)
mycon.commit()
space()
break
This function returns a list of products booked by a customer using the customer’s ID. It is used in other
functions to view or delete the booked orders of the customer.
def get_bkd_pro(cust_id):
where cust_id=%s;'
mycur.execute(qry, (cust_id,))
bp = mycur.fetchone()
bkd_pro = bp[0]
return bkd_pro
To view the products booked by customers, the get booked product function is used where customer ID
is passed as an argument.
It then checks if the customer has any bookings or not and then displays the result accordingly.
If more than one product is booked, the product IDs are stored as a single value in the table separated by
‘_’. The fetched values are then split and printed.
To book a new product, the function asks the customer to enter the product ID and then checks if the
product with the given ID exists in the products table or not. It then adds the product to the booked
products column of the customer’s table.
If the customer already has a booked product, the new product ID is concatenated with the existing ID
and again stored in the table.
For the customer to update their account, the function displays the existing customer details and then
asks them to enter the fields they want to update.
To cancel booked products the function asks for product ID and checks if it is booked or not and then
deletes it accordingly.
def sign_in():
try:
list_of_ids = check()
if ask in list_of_ids:
while True:
1) View Bookings
2) Book a product
if ccc == '1':
# is passed as an argument
s = get_bkd_pro(ask)
else:
print('Booked products')
for bkditems in d:
print(bkditems)
if ccc == '2':
mycur.execute(qry)
pro_list = mycur.fetchall()
list_of_products = []
for i in pro_list:
list_of_products.append(i[0])
mycur.execute(qry, (ask,))
pr = mycur.fetchone()
prl = pr[0]
mycur.execute(qry, val)
mycon.commit()
else:
prl1 = prl+pro_id
mycur.execute(qry2, val2)
mycon.commit()
else:
if ccc == '3':
mycur.execute(qry, (ask,))
clist = mycur.fetchone()
dic = {}
for i in range(4):
dic[flds[i]] = clist[i+1]
dic[flds[updtc-1]] = upval
yn = input(
if yn in 'Nn':
break
updtl = tuple(dic.values())+(ask,)
val = (updtl)
mycur.execute(qry, val)
mycon.commit()
if ccc == '4':
try:
bkd_pro = get_bkd_pro(ask)
else:
if cw in 'Aa':
where cust_id=%s'
mycur.execute(qry, (ask,))
mycon.commit()
elif cw in bkd_pro:
x = (bkd_pro[0:-1]).split('_')
x.remove(cw)
updt_pro = ''
for item in x:
updt_pro = updt_pro+item+'_'
mycur.execute(qry, val)
mycon.commit()
except Exception:
if ccc.lower() == 'back':
space()
break
else:
except Exception:
This function fetches all existing products from the database and then displays them in the form of a
table.
def view_pro():
mycur.execute(qry)
d = mycur.fetchall()
dic = {}
for i in d:
dic[i[0]] = i[1:]
print('_'*80)
print('_'*80)
for k, v in dic.items():
a, b, c = v
print('_'*80)
Add products function is used by the employees of the boutique to add new product details. It asks for a
product number, product ID, price, and stock from the employee and enters a new record in the
products table of the database.
# To add a new product in Products table
def addpro():
view_pro()
for j in range(n):
# product details.
t = ()
val = t
mycur.execute(qry, val)
mycon.commit()
print("Product Added")
This function is used by the employees of the boutique to delete product details. It asks for the product
ID and then deletes the record from the products table of the database.
# To delete a product from the table
def delpro():
view_pro()
mycur.execute(qry, (delt,))
mycon.commit()
print("Product is deleted")
This function is used for employees to login into their accounts. It allows employees to :
def emp_sign_in():
try:
mycur.execute(qry)
d = mycur.fetchall()
lis = []
for i in d:
lis.append(i[0])
else:
while True:
space()
3. Delete a product \n
if ccc == '1':
bkd_pro = get_bkd_pro(cust_id)
else:
if pro_id in bkd_pro:
x = (bkd_pro[0:-1]).split('_')
x.remove(pro_id)
updt_pro = ''
for i in x:
updt_pro = updt_pro+i+'_'
where cust_id=%s;'
mycur.execute(qry, val)
mycon.commit()
else:
addpro()
delpro()
break
except Exception:
Add employee function allows the employer to add a new employee to the boutique and insert the
records into the employee table of the database.
def addemp():
mycur.execute(qry)
emp_list = mycur.fetchall()
ne = []
t = ()
print('Address ')
# of employees to be added
ne = ne+[t, ]
for i in range(len(ne)):
val = ne[i]
mycur.execute(qry, val)
mycon.commit()
space()
This function is used for employer login and allows the employer to :
View all products
def employer():
while True:
print()
if ccc == '1':
view_pro()
if ccc == '2':
addemp()
if ccc.lower() == "back":
break
Main Program
The program first asks for a choice of the user to enter as a customer, employee, or employer and calls
the respective functions for the functioning of the program.
print('WELCOME !')
while True:
print('''Are you a :
(A). Customer
(B). Employee
(C). Employer
ch = input('Enter - ')
try:
if ch in 'aA':
if choice == '1':
cust_ac()
sign_in()
else:
if ch in 'bB':
emp_sign_in()
if ch in 'cC':
employer()
break
except Exception:
space()