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

Library Management System Project

library management system project for class 12 python mysql connectivity

Uploaded by

manu pro gaming
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
120 views

Library Management System Project

library management system project for class 12 python mysql connectivity

Uploaded by

manu pro gaming
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 32

Vivekanand public school

PROJECT FILE

MySQL connectivity with python

BY Aksh JAISWAL

Name: Aksh Jaiswal


Class: XII-B
Topic: Library Management System
Roll no: 02
Academic Year: 2024-25
CERTIFICATE

This is to certify that Aksh Jaiswal of class 12th – B has


successfully completed the Computer science project
on the Topic: library management system under the
guidance of Mrs. Anshu sharma mam during the
academic year 2024-25.

Signature of signature of
Internal examiner external examiner
__________ __________
ACKNOWLEDGMENT

I would like to express my special thanks of gratitude


to my teacher Anshu sharma mam as well our principal
Mrs. Nancy Khanna mam who gave me the golden
opportunity to do this project of Computer Science,
which also helped me in doing a lot of Research and I
came to know new things about it. Without their help,
guidance and support it would have been impossible to
complete this project.
Secondly, I would also like to thanks my parents and
friends who helped me a lot in finishing this project
within limited time. I am making this project not only
for marks but also to increase my knowledge.

Once again thanks to all who helped me in doing this


project?
INDEX
INTRODUCTION
FILES IMPORTED IN PROJECT
 Tables created in the MySQL
Python Source code
Output
Bibliography
INTRODUCTION

The project is designed to keeps records in a library. A


table named login in MySQL to store information about
user login ids and passwords.

One more table named available_books in MySQL to


store information about id, name, subject, quantity

Another table named books issued in MySQL to store


information about books issued, ids, title, issue date
and return date

Another table named books return in MySQL to store


information about books return, ids, title, issue date
and return date

Administrator of the project can enter new books


records, display all books details; he can modify and
delete records in the tables.
FILES IMPORTED IN PROJECT

1.) Import MySQL connector for database connectivity

FUNCATIONS USED IN PROJECT

1. Connect() - for database and table creation


2.) Cursor() - To execute MySQL queries.
3.) fetchall() - To fetch all the rows
4.) commit() – To execute(commit) current transaction.
5.)fetchone()- To fetch row according to query.
Tables created in the MySQL

Database: aksh_library_management

TABLE: AVAILABLE_BOOKS

Table: Issued
Table: Login

Table: Members
Python Source code:

import mysql.connector

from datetime import datetime, timedelta

# Connect to MySQL

mydb = mysql.connector.connect(host='localhost', user='root', passwd='aksh9906')

mycursor = mydb.cursor()

print("""

========================================================

Welcome To Library Management System by Aksh Jaiswal

========================================================

""")

# Creating Database and Tables

mycursor.execute("CREATE DATABASE IF NOT EXISTS aksh_library_management")

mycursor.execute("USE aksh_library_management")

mycursor.execute("""CREATE TABLE IF NOT EXISTS available_books (

id INT AUTO_INCREMENT PRIMARY KEY,

name VARCHAR(50),

subject VARCHAR(50),

quantity INT)""")

mycursor.execute("""CREATE TABLE IF NOT EXISTS issued (

id INT AUTO_INCREMENT PRIMARY KEY,


name VARCHAR(50),

subject VARCHAR(50),

s_name VARCHAR(50),

s_class VARCHAR(25),

issue_date DATE,

due_date DATE)""")

mycursor.execute("""CREATE TABLE IF NOT EXISTS login (

user VARCHAR(25),

password VARCHAR(25))""")

mycursor.execute("""CREATE TABLE IF NOT EXISTS members (

id INT AUTO_INCREMENT PRIMARY KEY,

name VARCHAR(50))""")

mydb.commit()

# Check if login credentials exist, if not, insert default credentials

mycursor.execute("SELECT * FROM login")

if mycursor.fetchone() is None:

mycursor.execute("INSERT INTO login VALUES('root', 'aksh9906')")

mydb.commit()

# Insert library members

members = [

'Aditya Choudhary',

'Akshay Sengar',

'Vikrant Tyagi',

'Chirag Goyal',
'Harshit Bhardwaj',

'Amandeep Singh',

'Divyansh Gupta',

'Devanshu Panchal',

'Kartikay Vashist',

'Kairav Choudhary',

'Ridhem Sehgal',

'Kartik',

'Dask Saini',

'Krish Burman'

mycursor.execute("SELECT * FROM members")

if mycursor.fetchone() is None:

for member in members:

mycursor.execute(f"INSERT INTO members (name) VALUES ('{member}')")

mydb.commit()

# Main working loop

while True:

print("""

1.) Login

2.) Exit

""")

ch = int(input("Enter your choice: "))

if ch == 1:
pas = input("Enter password: ")

mycursor.execute("SELECT * FROM login")

t_user, t_pas = mycursor.fetchone()

if pas == t_pas:

print("Login Successfully...")

loop1 = 'n'

while loop1 == 'n':

print("""

____________________________________________

1. Add new books

2. Remove any book

3. Update book details

4. Issue book to students

5. Return book

6. View available books

7. View issued books

8. View members

9. Remove member

10. Update member details

11. Logout

_____________________________________________

""")

ch = int(input("Enter your choice: "))

# Add New Book

if ch == 1:
loop2 = 'y'

while loop2 == 'y':

print("All information is mandatory to be filled!")

name = input("Enter book name: ")

subject = input("Enter subject: ")

quan = int(input("Enter quantity: "))

mycursor.execute(f"INSERT INTO available_books (name, subject, quantity)


VALUES ('{name}', '{subject}', {quan})")

mydb.commit()

print("Book added successfully...")

loop2 = input("Do you want to add another book? (y/n): ").lower()

loop1 = input("Do you want to logout? (y/n): ").lower()

# Remove Book

elif ch == 2:

idd = int(input("Enter ID to remove book: "))

mycursor.execute(f"SELECT * FROM available_books WHERE id = {idd}")

if mycursor.fetchone():

mycursor.execute(f"DELETE FROM available_books WHERE id = {idd}")

mydb.commit()

print("Book successfully removed...")

else:

print("Invalid book ID...")

loop1 = input("Do you want to logout? (y/n): ").lower()

# Update Book Details


elif ch == 3:

idd = int(input("Enter ID of the book to update: "))

mycursor.execute(f"SELECT * FROM available_books WHERE id = {idd}")

if mycursor.fetchone():

new_name = input("Enter new book name (or press enter to skip): ")

new_subject = input("Enter new subject (or press enter to skip): ")

new_quan = input("Enter new quantity (or press enter to skip): ")

if new_name:

mycursor.execute(f"UPDATE available_books SET name = '{new_name}'


WHERE id = {idd}")

if new_subject:

mycursor.execute(f"UPDATE available_books SET subject = '{new_subject}'


WHERE id = {idd}")

if new_quan:

mycursor.execute(f"UPDATE available_books SET quantity = {new_quan}


WHERE id = {idd}")

mydb.commit()

print("Book details updated successfully...")

else:

print("Invalid book ID...")

loop1 = input("Do you want to logout? (y/n): ").lower()

# Issue Book

elif ch == 4:

loop2 = 'y'
while loop2 == 'y':

idd = int(input("Enter Book ID: "))

s_name = input("Enter student name: ")

s_class = input("Enter student class: ")

issue_date = datetime.now().date()

due_date = issue_date + timedelta(days=7)

mycursor.execute(f"SELECT * FROM available_books WHERE id = {idd}")

book = mycursor.fetchone()

if book:

t_id, t_name, t_subject, t_quan = book

if t_quan > 0:

new_quan = t_quan - 1

mycursor.execute(f"""INSERT INTO issued

(id, name, subject, s_name, s_class, issue_date, due_date)

VALUES

({t_id}, '{t_name}', '{t_subject}', '{s_name}', '{s_class}',


'{issue_date}', '{due_date}')""")

mycursor.execute(f"UPDATE available_books SET quantity = {new_quan}


WHERE id = {t_id}")

mydb.commit()

print("Book successfully issued...")

else:

print("Book not available...")

else:

print("Invalid input...")

loop2 = input("Do you want to issue more books? (y/n): ").lower()

loop1 = input("Do you want to logout? (y/n): ").lower()


# Return Book

elif ch == 5:

loop2 = 'y'

while loop2 == 'y':

idd = int(input("Enter Book ID: "))

s_name = input("Enter student name: ")

s_class = input("Enter student class: ")

mycursor.execute(f"SELECT * FROM issued WHERE id = {idd} AND s_name =


'{s_name}' AND s_class = '{s_class}'")

issued_book = mycursor.fetchone()

if issued_book:

issue_date, due_date = issued_book[5], issued_book[6]

current_date = datetime.now().date()

days_overdue = (current_date - due_date).days

fine = 250 if days_overdue > 0 else 0

if fine > 0:

print(f"Book is overdue by {days_overdue} days. Fine: ₹{fine}")

mycursor.execute(f"DELETE FROM issued WHERE id = {idd} AND s_name =


'{s_name}' AND s_class = '{s_class}'")

mycursor.execute(f"SELECT * FROM available_books WHERE id = {idd}")

book = mycursor.fetchone()

t_id, t_name, t_subject, t_quan = book

new_quan = t_quan + 1

mycursor.execute(f"UPDATE available_books SET quantity = {new_quan}


WHERE id = {t_id}")

mydb.commit()
print("Book successfully returned...")

else:

print("Book not issued yet...")

loop2 = input("Do you want to return more books? (y/n): ").lower()

loop1 = input("Do you want to logout? (y/n): ").lower()

# View Available Books

elif ch == 6:

mycursor.execute("SELECT * FROM available_books")

print("ID | Name | Subject | Quantity")

for i in mycursor:

print(f"{i[0]} | {i[1]} | {i[2]} | {i[3]}")

loop1 = input("Do you want to logout? (y/n): ").lower()

# View Issued Books

elif ch == 7:

mycursor.execute("SELECT * FROM issued")

print("ID | Name | Subject | Student Name | Student Class | Issue Date |Due Date")

for i in mycursor:

print(f"{i[0]} | {i[1]} | {i[2]} | {i[3]} | {i[4]} | {i[5]} | {i[6]}")

loop1 = input("Do you want to logout? (y/n): ").lower()

# View Members

elif ch == 8:

mycursor.execute("SELECT * FROM members")

print("ID | Name")

for i in mycursor:
print(f"{i[0]} | {i[1]}")

loop1 = input("Do you want to logout? (y/n): ").lower()

# Remove Member

elif ch == 9:

idd = int(input("Enter Member ID to remove: "))

mycursor.execute(f"SELECT * FROM members WHERE id = {idd}")

if mycursor.fetchone():

mycursor.execute(f"DELETE FROM members WHERE id = {idd}")

mydb.commit()

print("Member successfully removed...")

else:

print("Invalid member ID...")

loop1 = input("Do you want to logout? (y/n): ").lower()

# Update Member Details

elif ch == 10:

idd = int(input("Enter Member ID to update: "))

mycursor.execute(f"SELECT * FROM members WHERE id = {idd}")

if mycursor.fetchone():

new_name = input("Enter new member name: ")

mycursor.execute(f"UPDATE members SET name = '{new_name}' WHERE id = {idd}")

mydb.commit()

print("Member details updated successfully...")

else:

print("Invalid member ID...")


loop1 = input("Do you want to logout? (y/n): ").lower()

# Logout

elif ch == 11:

break

else:

print("Incorrect Password!")

elif ch == 2:

print("Exiting...")

break

else:

print("Invalid choice!")
Outputs

Executing the code:

Login:
Adding new books:
Issuing Books:

Table “available_books” after issuing books:


Returning books:

Table “available_books” and table”issue_books” after


returning books:

Displaying available books:


Displaying issue books:
Removing books

: - Before removing the books

: - After removing the books


Updating books details

Before the update


After the update
Members table:-

Updating members table:-

Before the updating


After the updating
Deleting member

Result:-
Exiting the program:
Bibliography

Source:
 tutorialsPoint.com
LearnPython.org
www.python.org
Software:
MySQL
Python idle
Books:
Class 12 computer science by Preeti Arora book

You might also like