You are on page 1of 4

'''

Project : Library Book Record Management System


Softwares : MySQL & Python 3.8.5(IDLE), Pydroid 3
Team Members :

Databse name : library


Table name : book

Structure of table book :

bno integer - Book's Number Primary Key


bname varchar(30) - Book's Name
bauth varchar(40) - Book's Author
bprice integer - Book's Price
bqty integer - Book's Quantity

Pre requirements for the project :

1. softwares- Python 3.8.5 & Mysql must be installed on system.


2. Database "library" must exists on system
3. Table "book" must be exists in database "library"

'''

'''
# Code for creating database- library [Must be executed only ONCE]

import mysql.connector as sqlcon

mycon= sqlcon.connect(host="localhost", user="root", passwd="1234")


if mycon.is_connected():
print("databse connected successfully")
mycur=mycon.cursor()
qry="CREATE DATABASE library"
mycur.execute(qry)
print("Database created successfully")
'''

'''
# Code for creating table - book in the database- library [Must be executed only ONCE]

import mysql.connector as sqlcon

mycon= sqlcon.connect(host="localhost", user="root", passwd="1234", database="library")

mycur=mycon.cursor()

mycur.execute("CREATE TABLE book(bno integer primary key, bname varchar(50), bauth


varchar(50), bprice integer, bqty integer)")
print("Table created successfully")
'''

# MySQL Databse connection

import mysql.connector as sqlcon

mycon= sqlcon.connect(host="localhost", user="root", passwd="1234", database="library")


if mycon.is_connected():
print("Connected to MYSQL database successfully")
mycur= mycon.cursor()

# function to insert book record


def insertbook():
bno=int(input("Enter Book Code:"))
bname=input("Enter Book Name:")
bauth=input("Enter Book Author:")
bprice=int(input("Enter Book Price:"))
bqty=int(input("Enter Book Quantity:"))

qry="INSERT INTO book VALUES(%s,%s,%s,%s,%s)"


data=(bno,bname,bauth,bprice,bqty)
mycur.execute(qry,data)
mycon.commit()
print("\t\tRecord ADDED successfuly...")

# Function to display book record


def displaybook():
qry="SELECT * FROM book"
mycur.execute(qry)
data=mycur.fetchall()
count=mycur.rowcount
print("\t\t Total Book Records.........:",count,"\n")

for (bno,bname,bauth,bprice,bqty) in data:


print("Book Code:\t",bno)
print("Book Name:\t",bname)
print("Book Author:\t",bauth)
print("Book Price:\t",bprice)
print("Book Quantity:\t",bqty)
print(".................................")

# function to search book record


def searchbook():
bno=int(input("Enter book number to be searched...:"))
qry="SELECT * FROM book WHERE bno=%s"
rec=(bno,)
mycur.execute(qry,rec)
data=mycur.fetchall()
count=mycur.rowcount
if count!=0:
print("\t\t Book Records found.........:\n")
for (bno,bname,bauth,bprice,bqty) in data:
print("Book Code:\t",bno)
print("Book Name:\t",bname)
print("Book Author:\t",bauth)
print("Book Price:\t",bprice)
print("Book Quantity:\t",bqty)
print(".................................")

else:
print("\t\t\t Record NOT found..!!!")
# function to delete book record
def deletebook():
bno=int(input("Enter book number to be deleted...:"))
qry="SELECT * FROM book WHERE bno=%s"
rec=(bno,)
mycur.execute(qry,rec)
data=mycur.fetchall()
count=mycur.rowcount

if count!=0:
print("\t\t Book Records found.........:\n")
for (bno,bname,bauth,bprice,bqty) in data:
print("Book Code:\t",bno)
print("Book Name:\t",bname)
print("Book Author:\t",bauth)
print("Book Price:\t",bprice)
print("Book Quantity:\t",bqty)
print(".................................")

opt=input("Are you SURE to DELETE above record (Y/N)...:")


if opt=="Y" or opt=="y" :
qry= "DELETE FROM book WHERE bno=%s"
rec=(bno,)
mycur.execute(qry,rec)
print("\n\t\tRecord deleted Successfully...... ")
mycon.commit()

else:
print("\t\t\t Record NOT found..!!!")

# function to update book record


def updatebook():
bno=int(input("Enter book number to be updated...:"))
qry="SELECT * FROM book WHERE bno=%s"
rec=(bno,)
mycur.execute(qry,rec)
data=mycur.fetchall()
count=mycur.rowcount

if count!=0:a
print("\t\t Book Records found.........:\n")
for (bno,bname,bauth,bprice,bqty) in data:
print("Book Code:\t",bno)
print("Book Name:\t",bname)
print("Book Author:\t",bauth)
print("Book Price:\t",bprice)
print("Book Quantity:\t",bqty)
print(".................................")

opt=input("Are you SURE to UPDATE above record (Y/N)...:")


if opt=="Y" or opt=="y" :
print ("\n\t\tEnter new Data....")
bname=input("Enter New Book Name:")
bauth=input("Enter New Book Author:")
bprice=int(input("Enter New Book Price:"))
bqty=int(input("Enter New Book Quantity:"))
qry= ("UPDATE book SET bname=%s,bauth=%s,bprice=%s,bqty=%s WHERE bno=%s")
rec=(bname,bauth,bprice,bqty,bno)
mycur.execute(qry,rec)
print("\n\t\tRecord Updated Successfully...... ")
mycon.commit()

else:
print("\t\t\t Record NOT found..!!!")

# code to display menu


while True :
print("\n\t\t LIBRARY BOOK RECORD MANAGEMENT\n")
print("==================================")
print("\t\t 1. Add New Book record")
print("\t\t 2. Display Book record")
print("\t\t 3. Search Book record")
print("\t\t 4. Delete Book record")
print("\t\t 5. Update Book record")
print("\t\t 6. EXIT")
print("==================================")
choice=int(input("Enter choice 1-6:"))
if choice==1:
insertbook() # function called for inserting book
elif choice==2:
displaybook() # function called for displaying records of book(s)
elif choice==3:
searchbook() # function called for searching book
elif choice==4:
deletebook() # function called for deleting book
elif choice==5:
updatebook() # function called for updating book record
elif choice==6:
mycon.close()
print("\n Thanks have a nice day.........")
break
else :
print("\t!!!wrong choice... please enter choice 1-6:")

You might also like