0% found this document useful (0 votes)
10 views7 pages

CS GT Xii

Uploaded by

badag283
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)
10 views7 pages

CS GT Xii

Uploaded by

badag283
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/ 7

SQL CODE :

CREATE DATABASE library_db;

USE library_db;

CREATE TABLE library_records (

id INT AUTO_INCREMENT PRIMARY KEY,

student_name VARCHAR(100),

register_no VARCHAR(20),

book_name VARCHAR(100),

book_id VARCHAR(20),

issued_date DATE,

renewal_date DATE,

fine_amount DECIMAL(5, 2)

);
Python code ;

import mysql.connector
from datetime import datetime
from prettytable import PrettyTable

# Establish a connection to the MySQL database


def connect_db():
return mysql.connector.connect(
host="localhost", # Change to your MySQL host
user="root", # Change to your MySQL user
password="Mysql@123", # Change to your MySQL password
database="library_db"
)

# Function to add a book record


def add_record():
conn = connect_db()
cursor = conn.cursor()

student_name = input("Enter Student Name: ")


register_no = input("Enter Register Number: ")
book_name = input("Enter Book Name: ")
book_id = input("Enter Book ID: ")
issued_date = input("Enter Issued Date (YYYY-MM-DD): ")
renewal_date = input("Enter Renewal Date (YYYY-MM-DD): ")
fine_amount = input("Enter Fine Amount (if any): ")

sql = "INSERT INTO library_records (student_name, register_no,


book_name, book_id, issued_date, renewal_date, fine_amount) VALUES (%s,
%s, %s, %s, %s, %s, %s)"
values = (student_name, register_no, book_name, book_id, issued_date,
renewal_date, fine_amount)

cursor.execute(sql, values)
conn.commit()
print("Record added successfully!")

cursor.close()
conn.close()

# Function to view all records


def view_records():
conn = connect_db()
cursor = conn.cursor()
cursor.execute("SELECT * FROM library_records")
records = cursor.fetchall()

# Define the table headers


table = PrettyTable()
table.field_names = ["ID", "Student Name", "Register No", "Book Name",
"Book ID", "Issued Date", "Renewal Date", "Fine Amount"]

# Add rows to the table


for record in records:
table.add_row(record)

# Print the table


print(table)

cursor.close()
conn.close()

# Function to update a fine amount for a record


def update_fine(register_no, fine_amount):
conn = connect_db()
cursor = conn.cursor()

sql = "UPDATE library_records SET fine_amount = %s WHERE register_no


= %s"
values = (fine_amount, register_no)

cursor.execute(sql, values)
conn.commit()

print("Fine updated successfully!")

cursor.close()
conn.close()

# Function to delete a record


def delete_record(register_no):
conn = connect_db()
cursor = conn.cursor()

sql = "DELETE FROM library_records WHERE register_no = %s"


values = (register_no,)

cursor.execute(sql, values)
conn.commit()

print("Record deleted successfully!")

cursor.close()
conn.close()

# Main menu
def main_menu():
while True:
print("\nLibrary Management System")
print("1. Add Book Record")
print("2. View All Records")
print("3. Update Fine")
print("4. Delete Record")
print("5. Exit")
choice = input("Enter choice: ")

if choice == '1':
add_record()
elif choice == '2':
view_records()
elif choice == '3':
reg_no = input("Enter Register Number: ")
fine = input("Enter New Fine Amount: ")
update_fine(reg_no, fine)
elif choice == '4':
reg_no = input("Enter Register Number to delete: ")
delete_record(reg_no)
elif choice == '5':
print("Exiting...")
break
else:
print("Invalid choice. Please try again.")

if __name__ == "__main__":
main_menu()
Command promt:

## To install mysql and python connector


Pip install mysql-connector-python
(copy the above statement nd put it in the
command promt)
## to display the output in the table properly
Pip install prettytable
(copy the above statement nd put it in the
command promt)

You might also like