0% found this document useful (0 votes)
26 views

Cs Project

This Python code connects to a MySQL database and allows users to perform CRUD (create, read, update, delete) operations on recipe data. The user is prompted to enter login credentials and select an operation - adding, viewing, deleting, updating recipes or exiting. Based on the selection, appropriate SQL queries are executed to manipulate the recipe data in the database.

Uploaded by

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

Cs Project

This Python code connects to a MySQL database and allows users to perform CRUD (create, read, update, delete) operations on recipe data. The user is prompted to enter login credentials and select an operation - adding, viewing, deleting, updating recipes or exiting. Based on the selection, appropriate SQL queries are executed to manipulate the recipe data in the database.

Uploaded by

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

SOURCE CODE:

import mysql.connector as ms

import sys

hname = input("Enter MySQL Host Name(localhost): ")


un = input("Enter MySQL User Name: ")
pw = input("Enter MySQL Password: ")
db = input("Enter MySQL Database: ")

mycon=
ms.connect(host='{}'.format(hname),user='{}'.format(un),passwd='{}'.format(p
w),database='{}'.format(db))

if mycon.is_connected==False:
print("Error in Connecting")

cur=mycon.cursor()
print('''Choose a Number to Procceed
1. Add New Recipe
2. View Recipe
3. Delete Recipe
4. Update Recipe
5.Exit\n''')

inp=int(input("Enter Number: "))

if inp==1:
print("Adding a New Recipe to the List")

food=input("Enter Food Name: ")


ing=input("Enter Required Ingredients: ")
eq=input("Enter Required Equipments: ")
ins=input("Enter Instructions: ")
rat=int(input("Ratings from 1-5: "))
qry="insert into recipe values('{}','{}','{}','{}',{})".format(food,ing,eq,ins,rat)
cur.execute(qry)
mycon.commit()
count=cur.rowcount
print(count,"Record is Added to the List")
print("Press Enter to Continue")
input()

elif inp==2:
print('''1. View All Record
2. View Selected Record''')
view=int(input("Enter Number: "))

if view==1:
cur.execute("select* from recipe")
data=cur.fetchall()
for row in data:
print("------------------------------------------------------------","\n"
"FOOD:",row[0],'\n'
"INGREDIENTS:",row[1],'\n'
"EQUIPMENTS:",row[2],'\n'
"INSTRUCTIONS:",row[3],'\n'
"RATINGS:",row[4],"\n",
"------------------------------------------------------------")
print("Press Enter To Continue")
input()

elif view==2:
cur.execute("select* from recipe")
data=cur.fetchall()
avail=0
l=input("Enter Food: ")
for row in data:
if row[0]==l:
avail=1
print("------------------------------------------------------------","\n"
"FOOD:",row[0],'\n'
"INGREDIENTS:",row[1],'\n'
"EQUIPMENTS:",row[2],'\n'
"INSTRUCTIONS:",row[3],'\n'
"RATINGS:",row[4],"\n",
"------------------------------------------------------------")
else:
if avail==0:
print("record not found")
print("Press Enter To Continue")
input()

elif inp==3:
print("Deleting a Record")
cur.execute("select* from recipe")
data=cur.fetchall()
for row in data:
print("------------------------------------------------------------","\n"
"FOOD:",row[0],'\n'
"INGREDIENTS:",row[1],'\n'
"EQUIPMENTS:",row[2],'\n'
"INSTRUCTIONS:",row[3],'\n'
"RATINGS:",row[4],"\n",
"------------------------------------------------------------")
fid=input("Enter Food: ").lower()
delete=0
for row in data:
if row[0]==fid:
delete=1
qry="delete from recipe where food_name='{}'".format(fid)
cur.execute(qry)
mycon.commit()
print()
if delete==1:
print("Record Deleted Successfully")
else:
print("Record Not Available")
print()
cur.execute("select* from recipe")
data=cur.fetchall()
for row in data:
print("-----------------------------------------------------------","\n"
"FOOD:",row[0],'\n'
"INGREDIENTS:",row[1],'\n'
"EQUIPMENTS:",row[2],'\n'
"INSTRUCTIONS:",row[3],'\n'
"RATINGS:",row[4],"\n",
"-----------------------------------------------------------")
print("Press Enter To Continue")
input()

elif inp==4:
print("Updating a Record")
fid =(input("Enter Food: "))
qry = "Select * from recipe where food_name = '{}' order by
food_name".format(fid)
cur.execute(qry)
row = cur.fetchone()
count = cur.rowcount
print(count," record is searched successfully.")
print("-----------------------------------------------------------","\n"
"FOOD:",row[0],'\n'
"INGREDIENTS:",row[1],'\n'
"EQUIPMENTS:",row[2],'\n'
"INSTRUCTIONS:",row[3],'\n'
"RATINGS:",row[4],"\n",
"-----------------------------------------------------------")
food=input("Enter Food(new) or .(old): ")
ing=input("Enter Ingredients(new) or .(old): ")
eq=input("Enter Equipments(new) or .(old): ")
ins=input("Enter Instructions(new) or .(old): ")
if food == '.':
food = row[0]
if ing == '.':
ing = row[1]
if eq == '.':
eq = row[2]
if ins == '.':
ins = row[3]
qry="update recipe set food_name = '{}', ingredients = '{}', equipments =
'{}',\
instructions = '{}'where food_name = '{}'".format(food,ing,eq,ins,fid)
cur.execute(qry)
mycon.commit()
count = cur.rowcount
print(count," record is updated successfully.")
print("Press Enter to continue.")
input()
elif inp==5:
mycon.close()
print("Terminating Program")
sys.exit(0)

else:
print("Wrong Input Try Again")
print("Press Enter to continue.")
input()

You might also like