0% found this document useful (0 votes)
8 views14 pages

Librarymgmt

Computer project file Class 12

Uploaded by

pandeyvansh299
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views14 pages

Librarymgmt

Computer project file Class 12

Uploaded by

pandeyvansh299
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

Front page

Acknowledgement
certificate
Intriduction of project
Source Code

import mysql.connector

from datetime import datetime

# Connect to the database

def connect_to_db():

return mysql.connector.connect(

host="localhost", # Your MySQL host

user="root", # Your MySQL username

password="12345", # Your MySQL password

database="LibraryDB" # The name of your database

# Add a new book to the library

def add_book():

title = input("Enter book title: ")

author = input("Enter author name: ")

published_year = int(input("Enter published year: "))

genre = input("Enter genre: ")

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

connection = connect_to_db()

cursor = connection.cursor()

cursor.execute("""

INSERT INTO books (title, author, published_year, genre, quantity)

VALUES (%s, %s, %s, %s, %s)

""", (title, author, published_year, genre, quantity))

connection.commit()

print("Book added successfully!")

cursor.close()

connection.close()
# Add a new member

def add_member():

first_name = input("Enter first name: ")

last_name = input("Enter last name: ")

email = input("Enter email: ")

phone = input("Enter phone number: ")

connection = connect_to_db()

cursor = connection.cursor()

cursor.execute("""

INSERT INTO members (first_name, last_name, email, phone)

VALUES (%s, %s, %s, %s)

""", (first_name, last_name, email, phone))

connection.commit()

print("Member added successfully!")

cursor.close()

connection.close()

# Borrow a book

def borrow_book():

member_id = int(input("Enter member ID: "))

book_id = int(input("Enter book ID: "))

borrow_date = datetime.now().date()

connection = connect_to_db()

cursor = connection.cursor()

# Check if the book is available

cursor.execute("SELECT quantity FROM books WHERE book_id = %s", (book_id,))

book = cursor.fetchone()

if book and book[0] > 0:


# Update the quantity of the book

cursor.execute("""

UPDATE books

SET quantity = quantity - 1

WHERE book_id = %s

""", (book_id,))

cursor.execute("""

INSERT INTO transactions (book_id, member_id, borrow_date)

VALUES (%s, %s, %s)

""", (book_id, member_id, borrow_date))

connection.commit()

print("Book borrowed successfully!")

else:

print("Sorry, this book is not available.")

cursor.close()

connection.close()

# Return a book

def return_book():

transaction_id = int(input("Enter transaction ID: "))

return_date = datetime.now().date()

connection = connect_to_db()

cursor = connection.cursor()

# Get the book ID for the transaction

cursor.execute("SELECT book_id FROM transactions WHERE transaction_id = %s", (transaction_id,))

transaction = cursor.fetchone()

if transaction:

book_id = transaction[0]
cursor.execute("""

UPDATE books

SET quantity = quantity + 1

WHERE book_id = %s

""", (book_id,))

cursor.execute("""

UPDATE transactions

SET return_date = %s

WHERE transaction_id = %s

""", (return_date, transaction_id))

connection.commit()

print("Book returned successfully!")

else:

print("Transaction not found.")

cursor.close()

connection.close()

# View all books

def view_books():

connection = connect_to_db()

cursor = connection.cursor()

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]}, Year: {book[3]}, Genre: {book[4]}, Quantity: {book[5]}")

cursor.close()

connection.close()

# View all members

def view_members():
connection = connect_to_db()

cursor = connection.cursor()

cursor.execute("SELECT * FROM members")

members = cursor.fetchall()

print("Library Members:")

for member in members:

print(f"ID: {member[0]}, Name: {member[1]} {member[2]}, Email: {member[3]}, Phone: {member[4]}")

cursor.close()

connection.close()

# Main function to interact with the system

def main():

while True:

print("\nLibrary Management System")

print("1. Add Book")

print("2. Add Member")

print("3. Borrow Book")

print("4. Return Book")

print("5. View Books")

print("6. View Members")

print("7. Exit")

choice = input("Enter your choice: ")

if choice == "1":

add_book()

elif choice == "2":

add_member()

elif choice == "3":

borrow_book()

elif choice == "4":


return_book()

elif choice == "5":

view_books()

elif choice == "6":

view_members()

elif choice == "7":

break

else:

print("Invalid choice, please try again.")

if __name__ == "__main__":

main()

SQL DATABASE:

-- Create database

CREATE DATABASE LibraryDB;

-- Use the newly created database

USE LibraryDB;

-- Create a table for books

CREATE TABLE books (

book_id INT AUTO_INCREMENT PRIMARY KEY,

title VARCHAR(255) NOT NULL,

author VARCHAR(255) NOT NULL,

published_year INT NOT NULL,

genre VARCHAR(100),

quantity INT DEFAULT 0

);

-- Create a table for members


CREATE TABLE members (

member_id INT AUTO_INCREMENT PRIMARY KEY,

first_name VARCHAR(100),

last_name VARCHAR(100),

email VARCHAR(100) UNIQUE,

phone VARCHAR(15)

);

-- Create a table for transactions (borrowed books)

CREATE TABLE transactions (

transaction_id INT AUTO_INCREMENT PRIMARY KEY,

book_id INT,

member_id INT,

borrow_date DATE,

return_date DATE,

FOREIGN KEY (book_id) REFERENCES books(book_id),

FOREIGN KEY (member_id) REFERENCES members(member_id)

);

You might also like