Untitled Document 3
Untitled Document 3
Python Code:
import mysql.connector
con=mysql.connector.connect(host="localhost", user="root",
password="Siddha@2234", database="products")
if con.is_connected():
print("connection successful")
print("welcome to product table interface")
cur = con.cursor()
def creating_table():
query1 = '''
create table products (
SNO int(70) primary key,
PRODUCT_NAME varchar(70) not null,
PRODUCT_RANK varchar(70) not null,
COMPANY varchar(70),
PRICE int(70),
DATE_OF_MANUFACTURE date,
GST_PERCENTAGE int(70),
FINAL_PRICE int(70)
);
'''
cur.execute(query1)
print("an empty table has been created")
def adding_products():
opt = "y"
while opt == "y":
SNO = int(input("Enter the product serial number:"))
PRODUCT_NAME = input("Enter Product Name:")
COMPANY = input("Enter Company Name:")
DOM = input("Enter Date of Manufacture:")
FINAL_PRICE = 0
GST_PERCENTAGE = 0
print("The GST percentages are given below according
to their ranks:")
print("Rank 'A' --> 30%")
print("Rank 'B' --> 15%")
print("Rank 'C' --> 5%")
PRODUCT_RANK = input("Enter Product Rank:")
PRICE = int(input("Enter the product price:"))
if PRODUCT_RANK == "A":
FINAL_PRICE = PRICE + PRICE * 0.30
GST_PERCENTAGE = 30
elif PRODUCT_RANK == "B":
FINAL_PRICE = PRICE + PRICE * 0.15
GST_PERCENTAGE = 15
elif PRODUCT_RANK == "C":
FINAL_PRICE = PRICE + PRICE * 0.05
GST_PERCENTAGE = 5
cur.execute(query2)
con.commit()
print("Record insertion completed")
opt = input("Do you want to add another product detail
(y/n):")
def search_and_displaying_product_details():
opt1 = "y"
while opt1 == "y":
print("1: Display the entire table")
print("2: Display a particular record")
ch = int(input("Enter your choice:"))
if ch == 1:
query3 = "SELECT * FROM products;"
cur.execute(query3)
data = cur.fetchall()
if data == []:
print("The table is empty")
else:
for i in data:
print(i)
elif ch == 2:
no = int(input("Enter product serial number you
want to search:"))
query4 = "SELECT * FROM products WHERE
SNO = {}".format(no)
cur.execute(query4)
data1 = cur.fetchall()
if data1 == []:
print("The table is empty")
else:
print(data1)
def update_product_details():
opt3 = "y"
while opt3 == "y":
print("Select which attribute you want to modify:")
print("1: PRODUCT_NAME")
print("2: PRODUCT_RANK")
print("3: COMPANY_NAME")
print("4: PRICE")
print("5: DATE OF MANUFACTURE")
print("6: GST_PERCENTAGE")
if choice1 == 1:
m1 = int(input("Enter the serial number of the
record: "))
m2 = input("Enter the new product name: ")
query7 = "UPDATE products SET
PRODUCT_NAME = '{}' WHERE SNO = {}".format(m2,
m1)
cur.execute(query7)
con.commit()
print("Record updated.")
elif choice1 == 2:
m3 = int(input("Enter the serial number of the
record: "))
m4 = input("Enter the new product rank: ")
if m4 == "A":
query8 = "SELECT PRICE FROM products
WHERE SNO = {}".format(m3)
cur.execute(query8)
con.commit()
data3 = cur.fetchall()
for b in data3:
PRICE = b
if m4.upper() == "A":
PRICE1 = PRICE * 0.3
FINAL_PRICE = PRICE + PRICE1
GST_PERCENTAGE = 30
query9 = "update products set
PRODUCT_RANK='{}',GST_PERCENTAGE={} where
SNO={}".format(m4, GST_PERCENTAGE, m3)
cur.execute(query9)
con.commit()
print("record updated")
elif choice1 == 3:
m5 = int(input("enter the serial number of the record:"))
m6 = input("enter the new company name:")
query14 = "update products set COMPANY_NAME='{}'
where SNO={}".format(m6, m5)
cur.execute(query14)
con.commit()
print("record updated")
elif choice1 == 4:
m7 = int(input("enter the serial number of the record:"))
m8 = int(input("enter the new price amount:"))
query15 = "select PRODUCT_RANK from products
where SNO={}".format(m7)
cur.execute(query15)
data6 = cur.fetchall()
for i4 in data6:
for b3 in i4:
PRODUCT_RANK = b3
if PRODUCT_RANK == "A":
PRICE1 = m8 * 0.3
FINAL_PRICE = m8 + PRICE1
GST_PERCENTAGE = 30
query16 = "update products set
PRICE={},FINAL_PRICE={} where SNO={}".format(m8,
FINAL_PRICE, m7)
cur.execute(query16)
con.commit()
print("record updated")
elif PRODUCT_RANK == "B":
PRICE1 = m8 * 0.15
FINAL_PRICE = m8 + PRICE1
GST_PERCENTAGE = 15
query17 = "update products set
PRICE={},FINAL_PRICE={} where SNO={}".format(m8,
FINAL_PRICE, m7)
cur.execute(query17)
con.commit()
print("record updated")
elif PRODUCT_RANK == "C":
PRICE1 = m8 * 0.05
FINAL_PRICE = m8 + PRICE1
GST_PERCENTAGE = 5
query18 = "update products set
PRICE={},FINAL_PRICE={} where SNO={}".format(m8,
FINAL_PRICE, m7)
cur.execute(query18)
con.commit()
print("record updated")
elif choice1 == 5:
m9 = int(input("enter the serial number of the record:"))
m10 = input("enter the new date of manufacture:")
query19 = "update products set
DATE_OF_MANUFACTURE='{}' where
SNO={}".format(m10, m9)
cur.execute(query19)
con.commit()
print("record updated")
elif choice1 == 6:
m11 = int(input("enter the serial number of the record:"))
print("here are the gst values you can change (do not add
any other value which are shown below)")
print("Rank A --> 30%")
print("Rank B ---> 15%")
print("Rank C ---> 5%")
m12 = int(input("enter the new GST value:"))
query20 = "select PRICE from products where
SNO={}".format(m11)
cur.execute(query20)
for i5 in data7:
for b4 in i5:
PRICE = b4
if m12 == 30:
PRICE1 = PRICE * 0.3
FINAL_PRICE = PRICE + PRICE1
PRODUCT_RANK = "A"
elif m12 == 15:
PRICE1 = PRICE * 0.15
FINAL_PRICE = PRICE + PRICE1
PRODUCT_RANK = "B"
elif m12 == 5:
PRICE1 = PRICE * 0.05
FINAL_PRICE = PRICE + PRICE1
PRODUCT_RANK = "C"
if choice2 == 1:
creating_table()
elif choice2 == 2:
adding_products()
elif choice2 == 3:
search_and_displaying_product_details()
elif choice2 == 4:
remove_products()
elif choice2 == 5:
update_product_details()
elif choice2 == 6:
con.close()
break
opt4 = input("do you want to continue the
program(y/n):")