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

Python Code

Uploaded by

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

Python Code

Uploaded by

cognizen2
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

import mysql.

connector
from mysql.connector import Error
import datetime

# Connect to MySQL database


def connect_db():
try:
connection = mysql.connector.connect(
host='localhost', # Change this to your MySQL server if different
database='LibraryDB',
user='root', # Change this to your MySQL username
password='Hasnain' # Change this to your MySQL password
)
if connection.is_connected():
print("Connected to the database")
return connection
except Error as e:
print(f"Error: {e}")
return None

# Close database connection


def close_db_connection(connection):
if connection.is_connected():
connection.close()
print("Connection closed")

# Function to add a book to the database


def add_book(title, author, publisher, available_copies):
connection = connect_db()
cursor = connection.cursor()
try:
cursor.execute("INSERT INTO Books (title, author, publisher,
available_copies) VALUES (%s, %s, %s, %s)",
(title, author, publisher, available_copies))
connection.commit()
print("Book added successfully.")
except Error as e:
print(f"Error adding book: {e}")
finally:
cursor.close()
close_db_connection(connection)

# Function to add a student


def add_student(name, course, email):
connection = connect_db()
cursor = connection.cursor()
try:
cursor.execute("INSERT INTO Students (name, course, email) VALUES (%s, %s,
%s)", (name, course, email))
connection.commit()
print("Student added successfully.")
except Error as e:
print(f"Error adding student: {e}")
finally:
cursor.close()
close_db_connection(connection)

# Function to issue a book to a student


def issue_book(student_id, book_id):
connection = connect_db()
cursor = connection.cursor()
try:
# Check if the book is available
cursor.execute("SELECT available_copies FROM Books WHERE book_id = %s",
(book_id,))
available_copies = cursor.fetchone()[0]

if available_copies > 0:
# Issue the book and update the available copies
issue_date = datetime.date.today()
cursor.execute("INSERT INTO Transactions (student_id, book_id,
issue_date) VALUES (%s, %s, %s)",
(student_id, book_id, issue_date))
cursor.execute("UPDATE Books SET available_copies = available_copies -
1 WHERE book_id = %s", (book_id,))
connection.commit()
print("Book issued successfully.")
else:
print("Sorry, this book is currently unavailable.")
except Error as e:
print(f"Error issuing book: {e}")
finally:
cursor.close()
close_db_connection(connection)

# Function to return a book


def return_book(student_id, book_id):
connection = connect_db()
cursor = connection.cursor()
try:
return_date = datetime.date.today()
cursor.execute("UPDATE Transactions SET return_date = %s, status =
'returned' WHERE student_id = %s AND book_id = %s AND status = 'issued'",
(return_date, student_id, book_id))
cursor.execute("UPDATE Books SET available_copies = available_copies + 1
WHERE book_id = %s", (book_id,))
connection.commit()
print("Book returned successfully.")
except Error as e:
print(f"Error returning book: {e}")
finally:
cursor.close()
close_db_connection(connection)

# Function to view books


def view_books():
connection = connect_db()
cursor = connection.cursor()
try:
cursor.execute("SELECT * FROM Books")
books = cursor.fetchall()
print("Books in the library:")
for book in books:
print(f"ID: {book[0]}, Title: {book[1]}, Author: {book[2]}, Publisher:
{book[3]}, Available Copies: {book[4]}")
except Error as e:
print(f"Error fetching books: {e}")
finally:
cursor.close()
close_db_connection(connection)

# Function to view students


def view_students():
connection = connect_db()
cursor = connection.cursor()
try:
cursor.execute("SELECT * FROM Students")
students = cursor.fetchall()
print("Students enrolled in the library system:")
for student in students:
print(f"ID: {student[0]}, Name: {student[1]}, Course: {student[2]},
Email: {student[3]}")
except Error as e:
print(f"Error fetching students: {e}")
finally:
cursor.close()
close_db_connection(connection)

# Function to view transactions (issued books)


def view_transactions():
connection = connect_db()
cursor = connection.cursor()
try:
cursor.execute("SELECT * FROM Transactions WHERE status = 'issued'")
transactions = cursor.fetchall()
print("Issued books:")
for transaction in transactions:
print(f"Transaction ID: {transaction[0]}, Student ID: {transaction[1]},
Book ID: {transaction[2]}, Issue Date: {transaction[3]}")
except Error as e:
print(f"Error fetching transactions: {e}")
finally:
cursor.close()
close_db_connection(connection)

# Main menu
def main():
while True:
print("\nLibrary Management System")
print("1. Add Book")
print("2. Add Student")
print("3. Issue Book")
print("4. Return Book")
print("5. View Books")
print("6. View Students")
print("7. View Issued Books")
print("8. Exit")

choice = input("Enter your choice: ")

if choice == '1':
title = input("Enter book title: ")
author = input("Enter book author: ")
publisher = input("Enter book publisher: ")
available_copies = int(input("Enter available copies: "))
add_book(title, author, publisher, available_copies)
elif choice == '2':
name = input("Enter student name: ")
course = input("Enter student course: ")
email = input("Enter student email: ")
add_student(name, course, email)
elif choice == '3':
student_id = int(input("Enter student ID: "))
book_id = int(input("Enter book ID: "))
issue_book(student_id, book_id)
elif choice == '4':
student_id = int(input("Enter student ID: "))
book_id = int(input("Enter book ID: "))
return_book(student_id, book_id)
elif choice == '5':
view_books()
elif choice == '6':
view_students()
elif choice == '7':
view_transactions()
elif choice == '8':
print("Exiting...")
break
else:
print("Invalid choice. Please try again.")

if _name_ == "_main_":
main()

You might also like