Menu.
py
def MenuBook():
while True:
SBook.clrscreen()
print("\t\t\t SlamBook\n")
print("=====================================================")
print("1. Add Record ")
print("2. Display Records ")
print("3. Search Record ")
print("4. Delete Record ")
print("5. Update Record ")
print("6. Return to Main Menu ")
print("=====================================================")
choice=int(input("Enter Choice between 1 to 5-------> : "))
if choice==1:
SBook.insertData()
elif choice==2:
SBook.display()
elif choice==3:
SBook.Searchfrnd()
elif choice==4:
SBook.deletedata()
elif choice==5:
print("No such Function")
elif choice==6:
return
else:
print("Wrong Choice......Enter Your Choice again")
x=input("Enter any key to continue")
#----------------------------------------------------------------------------------------
Slam_Book .py
import Menu
import SBook
while True:
SBook.clrscreen()
print("\t\t\t Slam Book Management\n")
print("=====================================================")
Menu.MenuBook()
print("====================================================")
SBOOK.py
import mysql.connector
from mysql.connector import errorcode
from datetime import date, datetime, timedelta
from mysql.connector import (connection)
import os
import platform
def clrscreen():
if platform.system()=="Windows":
print(os.system("cls"))
def display():
try:
os.system('cls')
cnx = connection.MySQLConnection(user='root', password='mysql', host='localhost',
database='slambook')
Cursor = cnx.cursor()
query = ("SELECT * FROM friends")
Cursor.execute(query)
for (id,name,addess,phno) in Cursor:
print("--------------------------------------------------------------------")
print("id : ",id)
print("Name : ",name)
print("address : ",address)
print("phno : ",phno)
print("------------------------------------------------------------")
Cursor.close()
cnx.close()
print("completed !!!!!!")
except mysql.connector.Error as err:
if err.errno == errorcode.ER_ACCESS_DENIED_ERROR: print("Something is wrong
with your user name or password") elif err.errno ==
errorcode.ER_BAD_DB_ERROR: print("Database does not exist")
else:
print(err)
else:
cnx.close()
def insertData():
try:
cnx = connection.MySQLConnection(user='root',
password='mysql',
host='127.0.0.1',
database=‘slambook’)
Cursor = cnx.cursor()
id=input("Enter id : ")
name=input("Enter Name : ")
add=input("Enter address : ")
ph=int(input("Enter phno : "))
"VALUES (%s, %s, %s, %s, %s, %s, %s)")
data = (id,name,add,ph)
Cursor.execute(Qry,data)
Make sure data is committed to the database cnx.commit()
Cursor.close()
cnx.close()
print("data saved successfully............")
except mysql.connector.Error as err:
if err.errno == errorcode.ER_ACCESS_DENIED_ERROR:
print("Something is wrong with your user name or password")
elif err.errno == errorcode.ER_BAD_DB_ERROR:
print("Database does not exist")
else:
print(err)
cnx.close()
def deletedata():
try:
cnx = connection.MySQLConnection(user='root',
password='mysql',
host='127.0.0.1',
database=‘slambook’)
Cursor = cnx.cursor()
id=input("Enter id of friend to be deleted : ")
Qry =("""DELETE FROM Friends WHERE id = %s""")
del_rec=(id,)
Cursor.execute(Qry,del_rec)
Make sure data is committed to the database cnx.commit()
Cursor.close()
cnx.close()
print(Cursor.rowcount,"Record(s) Deleted Successfully.............")
except mysql.connector.Error as err:
if err.errno == errorcode.ER_ACCESS_DENIED_ERROR:
print("Something is wrong with your user name or password")
elif err.errno == errorcode.ER_BAD_DB_ERROR:
print("Database does not exist")
else:
print(err)
cnx.close()
def Searchfrnd():
try:
cnx = connection.MySQLConnection(user='root',
password='mysql',
host='127.0.0.1',
database=‘slambook’)
Cursor = cnx.cursor()
id=input("Enter id be Searched from the friends : ")
query = ("SELECT * FROM Friends where id = %s ")
rec_srch=(id,)
Cursor.execute(query,rec_srch)
Rec_count=0
for (Bno,Bname,Author,price,publ,qty,d_o_purchase) in Cursor:
Rec_count+=1
for (id,name,addess,phno) in Cursor:
print("--------------------------------------------------------------------")
print("id : ",id)
print("Name : ",name)
print("address : ",address)
print("phno : ",phno)
print("------------------------------------------------------------")
if Rec_count%2==0:
input("Press any key to continue")
clrscreen()
print(Rec_count, "Record(s) found")
Make sure data is committed to the database cnx.commit()
Cursor.close()
cnx.close()
except mysql.connector.Error as err:
if err.errno == errorcode.ER_ACCESS_DENIED_ERROR:
print("Something is wrong with your user name or password")
elif err.errno == errorcode.ER_BAD_DB_ERROR:
print("Database does not exist")
else:
print(err)
cnx.close()
def Updatefrnd():
try:
cnx = connection.MySQLConnection(user='root',
password='mysql',
host='127.0.0.1',
database=‘slambook’)
Cursor = cnx.cursor()
id=input("Enter id of frined to be Updated : ")
query = ("SELECT * FROM Friends where id = %s ")
rec_srch=(id,)
print("Enter new data ")
name=input("Enter new Name : ")
add=input("Enter address : ")
ph=input("Enter phno: ")
Qry = ("UPDATE Friends SET name=%s,address=%s,"\ "phno=%s "\
"WHERE id=%s")
data = (name,add,ph, bno)
Cursor.execute(Qry,data)
Make sure data is committed to the database''' cnx.commit()
Cursor.close()
cnx.close()
print(Cursor.rowcount,"Record(s) Updated Successfully.............")
except mysql.connector.Error as err:
if err.errno == errorcode.ER_ACCESS_DENIED_ERROR:
print("Something is wrong with your user name or password")
elif err.errno == errorcode.ER_BAD_DB_ERROR:
print("Database does not exist")
else:
print(err)
cnx.close()
Updatefrnd()