0% found this document useful (0 votes)
5 views13 pages

Untitled Document 3

Uploaded by

meetkict
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views13 pages

Untitled Document 3

Uploaded by

meetkict
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

SOURCE CODE

Creation of database in MYSQL

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

query2 = """INSERT INTO products VALUES ({}, '{}',


'{}', '{}', {}, '{}', {}, {})""".format(
SNO, PRODUCT_NAME, PRODUCT_RANK,
COMPANY, PRICE, DOM, GST_PERCENTAGE,
FINAL_PRICE)

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)

opt1 = input("Do you want to continue using the search


engine to get product details (y/n):")
def remove_products():
opt2 = "y"
while opt2 == "y":
choice = int(input("Enter the product record serial
number you want to remove from the table: "))
query5 = "DELETE FROM products WHERE SNO =
{}".format(choice)
query6 = "SELECT * FROM products WHERE SNO =
{}".format(choice)
cur.execute(query6)
data2 = cur.fetchall()
if data2 == []:
print("The table is empty (no elements are there to
be removed).")
else:
print("The removed data is:", data2)
cur.execute(query5)
con.commit()
opt2 = input("Do you want to continue removing the
product details (y/n): ")

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")

choice1 = int(input("Select the attribute number you


want to modify: "))

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 m4.upper() == "B":


query10 = "select PRICE from products where
SNO={}".format(m3)
cur.execute(query10)
data4 = cur.fetchall()
for i2 in data4:
for b1 in i2:
PRICE = b1
PRICE1 = PRICE * 0.15
FINAL_PRICE = PRICE + PRICE1
GST_PERCENTAGE = 15
query11 = "update products set
PRODUCT_RANK='{}',GST_PERCENTAGE={} where
SNO={}".format(m4, GST_PERCENTAGE, m3)
cur.execute(query11)
con.commit()
print("record updated")
elif m4.upper() == "C":
query12 = "select PRICE from products where
SNO={}".format(m3)
cur.execute(query12)
data5 = cur.fetchall()
for i3 in data5:
for b2 in i3:
PRICE = b2
PRICE1 = PRICE * 0.05
FINAL_PRICE = PRICE + PRICE1
GST_PERCENTAGE = 5
query13 = "update products set
PRODUCT_RANK='{}',GST_PERCENTAGE={} where
SNO={}".format(m4, GST_PERCENTAGE, m3)
cur.execute(query13)
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"

query21 = "update products set


GST_PERCENTAGE={},PRODUCT_RANK='{}',FINAL_P
RICE={} where SNO={}".format(
m12, PRODUCT_RANK, FINAL_PRICE, m11
)
cur.execute(query21)
con.commit()
print("record updated")
else:
print("invalid option/// try again")
opt3 = input("do you want to continue modifying the product
table(y/n):")

print("you can perform any operations in this program on the


product table")
opt4 = "y"

while opt4 == "y":


print("1:create table")
print("2:addition of products")
print("3:search and display of product details")
print("4:deletion of products")
print("5:updation of products")
print("6:exit")
choice2 = int(input("enter your choice:"))

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):")

You might also like