0% found this document useful (0 votes)
14 views18 pages

CSFINAL1

This document describes a Python script that implements a basic library management system using a MySQL database. The script connects to a MySQL database, defines functions to execute SQL commands and fetch data, and provides a user interface to view book and borrower details, lend books, and make changes to the database tables.

Uploaded by

kingthenakul
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)
14 views18 pages

CSFINAL1

This document describes a Python script that implements a basic library management system using a MySQL database. The script connects to a MySQL database, defines functions to execute SQL commands and fetch data, and provides a user interface to view book and borrower details, lend books, and make changes to the database tables.

Uploaded by

kingthenakul
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/ 18

1

LIBRARY MANAGEMENT
SYSTEM

COMPUTER PROJECT

Submitted by
NAKUL T

in partial fulfillment for the award of the internal

marks for the AISSCE-2024

in

Computer Science

Subject Code: 083

SUGUNA PIP SCHOOL


COIMBATORE
2

BONAFIDE CERTIFICATE

Certified that this project “LIBRARY MANAGEMENT SYSTEM” is the


bonafide work of “NAKUL T” who carried out the project work under my
supervision.

SIGNATURE OF SIGNATURE OF
INTERNAL EXAMINER EXTERNAL EXAMINER

SIGNATURE OF
PRINCIPAL
3

DECLARATION

I NAKUL T of class XII-A3, hereby declare that the project titled “LIBRARY
MANAGEMENT SYSTEM” submitted to SUGUNA PIP SCHOOL,
(Affiliation Number: 1930213). Nehru Nagar, Kalapatti Road, Coimbatore,
Tamil Nadu - 641014, in regard to the Internal Assessment for class XII, is
a record of original project work done by me, under the supervision of
Mrs.ALLPIOUS R, faculty in the Information Technology Department,
Suguna Pip School.

I further certify that the work contained in the report is original and
has been done by me under the supervision of the faculty.

Student Name: NAKUL T

Register No. : 5995


4

ACKNOWLEDGEMENT

I extend my sincere gratitude to Mr. POOVANNAN, Principal of Suguna Pip


School Coimbatore, for his unwavering support throughout the development of
this Python program.

Special appreciation goes to my Faculty Guide, Mrs. ALLPIOUS, for her


invaluable guidance and mentorship during every phase of the project. Her
insightful lectures played a crucial role in comprehending the intricate concepts
related to data analysis and visualization using Python libraries.

The opportunity for extensive research provided in the project significantly


enriched my understanding of the subject matter, and I am genuinely indebted
to Mrs. ALLPIOUS for facilitating this learning experience.

I would like to express my heartfelt thanks to my parents, friends, and all those
who have been a constant source of support, both directly and indirectly, during
the course of this project.

Student Name. NAKUL T

Register No : 5995

Class: XII-A3

Date of Submission:
5

ABSTRACT

This Python script is a basic implementation of a library management


system using MySQL as the database. The script uses the
mysql.connector library to connect to a MySQL database running on
localhost.

Database Connection:

The script establishes a connection to a MySQL database server on


localhost using the mysql.connector library. If the connection is
successful, it prints a success message; otherwise, it prints an error
message.

Database Setup:

It creates a database named "pathsala" if it does not exist and selects this
database for further operations. Two tables are created: "books" and
"BORROWER" to store information about books and borrowers.

Functions:

The script defines several functions for executing SQL commands and
fetching data from the database. Functions like command, fetch, all_data,
detail_burrower, days_between, price_book, lend, borrowers, tfine,
insert, update, and close are implemented to perform various actions.

Actions:

The main actions available in the library management system include


viewing details of available books, checking details of a particular book,
lending a book, adding new books, updating data, viewing details of
borrowers, and committing changes to the database. For lending a book,
the script prompts the user to select a book from the available books, enter
borrower details, and updates the database accordingly.

User Interface:

The script provides a simple text-based user interface where users can
6

choose different actions by entering corresponding options.

Main Loop:

The script has a main loop (action_list()) that continuously prompts the
user to choose an action until the user decides to exit.

Exiting:

When the user chooses to exit, the script commits all changes to the
database, closes the database connection, and terminates the program.
7

TABLE OF CONTENTS

ABSTRACT……………………………………………………………………………………………5
SOURCE CODE…………………………………………………………………………………8
OUTPUT…………………………………………………………………………………………………14
SCOPE OF ENHANCEMENT……………………………………………………16
CONCLUSION……………………………………………………………………………………17
BIBLIOGRAPHY……………………………………………………….………………………18

TEACHER'S SIGNATURE
8

SOURCE CODE :
import mysql.connector as sqlctr
import sys
from datetime import datetime
mycon = sqlctr.connect(host='localhost', user='root', password='admin')
if mycon.is_connected():
print('\n')
print('Successfully connected to localhost')
else:
print('Error while connecting to localhost')
cursor = mycon.cursor()
#creating database
cursor.execute("create database if not exists pathsala")
cursor.execute("use pathsala")
#creating the tables we need
cursor.execute("create table if not exists books(SN int(5) primary key,Book_Name varchar(30),
Quantity_Available int(10),Price_Per_Day int(10))")
cursor.execute("create table if not exists BORROWER(SN int(5),borrowers_name
varchar(40),book_lent varchar(20),contact_no int(10))")
def command(st):
cursor.execute(st)
def fetch():
data = cursor.fetchall()
for i in data:
print(i)
def all_data(tname):
li = []
st = 'desc '+tname
command(st)
data = cursor.fetchall()
for i in data:
li.append(i[0])
st = 'select * from '+tname
command(st)
print('\n')
print('-------ALL_DATA_FROM_TABLE_'+tname+'_ARE ------ \n')
print(tuple(li))
fetch()
def detail_burrower(name,contact):
tup=('SN','borrowers_name','book_lent','date','contact_no')
print('\n---Details for borrower '+name+' -- \n')
print(tup)
st='select * from borrower where borrowers_name like "{}" and
contact_no={}'.format(name,contact)
command(st)
fetch()
def days_between(d1, d2):
d1 = datetime.strptime(d1, "%Y-%m-%d")
d2 = datetime.strptime(d2, "%Y-%m-%d")
global days
days=abs((d2 - d1).days)
9

def price_book(days,book_name):
st1 = 'select Price_Per_Day from books where Book_Name="{}"'.format(book_name)
command(st1)
data = cursor.fetchall()
for i in data:
global t_price
t_price=int(i[0])*days
print('No. of days {} book is kept : {}'.format(book_name,days))
print('Price per day for book {} is Rs.{}'.format(book_name,i[0]))
print('Total fare for book '+book_name +'-',t_price)
def lend():
flag='True'
while flag=='True':
print('\n AVAILABLE BOOKS \n')
st0 = 'select Book_Name from books where Quantity_Available>=1'
command(st0)
fetch()
st1='select max(SN) from borrower'
command(st1)
data_sn=cursor.fetchall()
for i in data_sn:
SN=i[0]+1
book_selected=str(input('Enter name of book from above list : '))
borrowers_name=str(input('Enter Borrower Name : '))
date=str(input('Enter date (YYYY-MM-DD) : '))
contact=int(input('Enter contact no. : '))
st_insert='insert into borrower
values({},"{}","{}","{}",{})'.format(SN,borrowers_name,book_selected,date,contact)
command(st_insert)
st_quantity='select quantity_available from books where
book_name="{}"'.format(book_selected)
command(st_quantity)
data_quantity=cursor.fetchall()
for quantity in data_quantity:
qty=quantity[0]-1
st_dec='update books set quantity_available={} where
book_name="{}"'.format(qty,book_selected)
command(st_dec)
dec=str(input('Do you want to add more records (Y/N) : '))
if dec.upper=="Y":
flag= 'True'
else:
flag='False'

def borrowers():
print('\n\n OPTIONS AVAILABLE \n\nEnter 1 : To Show detail of all borrowers \nEnter 2
: To check detail of a particular borrower \nEnter 3 : To calculate total fine of a borrower
\nEnter 4 : To go Back \nEnter 5 : To commit all the changes and exit')
dec = input('enter your choice-')
if dec=='1':
10

all_data('borrower')
elif dec=='2':
name = str(input('\nenter borrower name-'))
contact = str(input('enter borrower contact no.-'))
detail_burrower(name,contact)
elif dec=='3':
tfine()
elif dec=='4':
action_list()
elif dec=='5':
close()
borrowers()
def tfine():
name=str(input('\nEnter borrower name : '))
contact=input('Enter borrower contact_no : ')
detail_burrower(name, contact)
st1 = 'select book_lent from borrower where borrowers_name ="{}" and
contact_no={}'.format(name,contact)
command(st1)
data=cursor.fetchall()
for i in data:
book_name=i[0]
st2 = 'select date from borrower where borrowers_name="{}" and
book_lent="{}"'.format(name,book_name)
command(st2)
data1=cursor.fetchall()
for date in data1:
date_taken=date[0]
date_return = str(input('\nEnter returning date for book "{}" (YYYY-MM-DD) ,
Press ENTER to skip-'.format(book_name)))
while date_return!='':
days_between(str(date_return),str(date_taken))
price_book(days,i[0])
print('\nEnter Y : If Rs.{} is paid and book is returned.\nEnter N : If fare
is not paid and book is not returned.'.format(t_price))
dec=str(input('Enter (Y?N) : '))
if dec.upper()=="Y":
st= 'select SN , Quantity_Available from books where Book_Name
="{}"'.format(i[0])
command(st)
data2=cursor.fetchall()
for price in data2:
update('books', 'Quantity_Available',price[1]+1,price[0])
st_del = 'delete from borrower where borrowers_name="{}" and
book_lent="{}"'.format(name,book_name)
command(st_del)
break
else:
print("\n\nPLEASE PAY THE FARE AND RETURN BOOK AFTER READING.\n\n")
break
11

def insert():
flag = 'true'
while flag=='true':
licol=[]
li1=[]
li_val=[]
command('desc books')
data=cursor.fetchall()
for i in data:
licol.append(i[0])
command('select max(SN) from books')
dta=cursor.fetchall()
for j in dta:
li_val.append(j[0]+1)
for k in range(1,4):
val = str(input('Enter '+licol[k]+'-'))
li_val.append(val)
li1.append(tuple(li_val))
values = ', '.join(map(str, li1))
st1 = "INSERT INTO books VALUES {}".format(values)
command(st1)
all_data('books')
print('\n')
print("\nDATA INSERTED SUCCESSFULLY\n")
dec = str(input('Do u want to insert more data?(Y/N)-'))
if dec.upper() == "Y":
flag='true'
else:
flag='false'
action_list()
def update(tname,col1,post_value,pre_value):
st = str('update %s set %s=%s where SN=%s') % (tname, col1, "'%s'", "'%s'") %
(post_value, pre_value)
command(st)
all_data(tname)
print('\nVALUE UPDATED SUCCESSFULLY')

def close():
mycon.commit()
mycon.close()
if mycon.is_connected():
print('still connected to localhost')
else:
print('\n\nconnection closed successfully.')
sys.exit()
def action_list():
print('\n')
print('#### WELCOME TO LIBRARY MANAGEMENT SYSTEM ####\n\nEnter 1 : To View details of all
available Books\nEnter 2 : To check detail of a particular book\nEnter 3 : To lend a book
12

\nEnter 4 : To add new books in list \nEnter 5 : To update data \nEnter 6 : To view details
of borrowers \nEnter 7 : To commit all changes and exit')
dec = input('\nenter your choice-')
if dec == '1':
all_data('books')
elif dec=='2':
tup=('SN','Book_Name','Quantity_Available','Price_Per_Day')
tup1 = ('SN', 'borrowers_name', 'book_lent', 'contact_no')
in1=str(input('enter first name , last name or middle name of a book-'))
print('\n ALL DATA OF BOOKS HAVING "{}" IN THEIR NAME FROM BOTH
TABLE '.format(in1))
st =str('select * from books where book_name like "{}"'.format('%'+in1+'%'))
st1=str('select * from borrower where book_lent like "{}"'.format('%'+in1+'%'))
print('\n DATA FROM TABLE BOOKS \n')
command(st)
print(tup)
fetch()
print('\n DATA FROM TABLE BORROWER \n')
command(st1)
print(tup1)
fetch()
print()
elif dec == '3':
lend()
elif dec=='4':
insert()
elif dec=='5':
flag='true'
while flag=='true':
tname = 'books'
li = []
st1 = 'desc '+tname
command(st1)
data = cursor.fetchall()
for i in data:
li.append(i[0])
all_data(tname)
print('\n columns in table '+tname+' are')
print(li)
col1 = str(input('enter column name for modification from above list-'))
lipo = ['SN']
lipo.append(col1)
print(tuple(lipo))
st0 = 'select SN , %s from books' % (col1)
command(st0)
fetch()
pre_value = str(input('enter corresponding SN for the data to be changed-'))
post_value = str(input('enter new value for column %s having SN %s-' % (col1,
pre_value)))
update(tname, col1, post_value, pre_value)
13

dec = str(input('Do you want to change more


data?(Y/N)-'))if dec == 'y' or dec == 'Y':
flag='
true'else:
flag='false'

elif
dec=='
6':
borrow
ers()
elif
14

OUTPUT:

Successfully connected to localhost

#### WELCOME TO LIBRARY MANAGEMENT SYSTEM ####

Enter 1 : To View details of all available Books


Enter 2 : To check detail of a particular book
Enter 3 : To lend a book
Enter 4 : To add new books in list
Enter 5 : To update data
Enter 6 : To view details of borrowers
Enter 7 : To commit all changes and exit

enter your choice-1

-------ALL_DATA_FROM_TABLE_books_ARE-------

('SN', 'Book_Name', 'Quantity_Available', 'Price_Per_Day')

#### WELCOME TO LIBRARY MANAGEMENT SYSTEM ####

Enter 1 : To View details of all available Books


Enter 2 : To check detail of a particular book
Enter 3 : To lend a book
Enter 4 : To add new books in list
Enter 5 : To update data
Enter 6 : To view details of borrowers
Enter 7 : To commit all changes and exit

enter your choice-2


enter first name , last name or middle name of a book-python

___ALL DATA OF BOOKS HAVING "python" IN THEIR NAME FROM BOTH TABLE____

__DATA FROM TABLE BOOKS__


15

('SN', 'Book_Name', 'Quantity_Available', 'Price_Per_Day')

__DATA FROM TABLE BORROWER__

('SN', 'borrowers_name', 'book_lent', 'contact_no')

#### WELCOME TO LIBRARY MANAGEMENT SYSTEM ####

Enter 1 : To View details of all available Books


Enter 2 : To check detail of a particular book
Enter 3 : To lend a book
Enter 4 : To add new books in list
Enter 5 : To update data
Enter 6 : To view details of borrowers
Enter 7 : To commit all changes and exit

enter your choice-3

___AVAILABLE BOOKS___
16

SCOPE OF ENHANCEMENT :

Implement exception handling to catch and handle potential errors that may
occur during database operations. This will make your code more robust.
Break down your code into smaller functions with specific responsibilities.
This will make your code more modular, readable, and easier to maintain.
Implement input validation to ensure that user inputs are in the expected
format. This will prevent issues related to invalid inputs. Use parameterized
queries instead of string formatting to prevent SQL injection attacks. This is
important for security. Consider implementing automated tests to ensure
the correctness of your functions and to make future updates safer.
17

CONCLUSION :

This Python code is an implementation of a simple library management


system using MySQL as the database. The code connects to a MySQL
database, creates tables for books and borrowers, and provides
functionalities such as viewing details of available books, checking details of
a particular book, lending a book, adding new books to the list, updating
data, viewing details of borrowers, and committing changes to the database.
18

BIBLIOGRAPHY :

COMPUTER SCIENCE WITH PYTHON – TEXTBOOK FOR CLASS 11 BY SUMITA ARORA


COMPUTER SCIENCE WITH PYTHON – TEXTBOOK FOR CLASS 12 BY SUMITA ARORA

You might also like