technical skills grow

Responsive Ads Here

Wednesday, May 21, 2025

PROJECT : CLI Library Management System in Python

 

import psycopg2
from datetime import date

conn = psycopg2.connect(
    dbname="library", user="postgres", password="pgsql@123", host="localhost"
)

cur = conn.cursor()
 
def add_book (title , author):
    cur.execute("insert into books (title,author) values (%s,%s)",
(title,author))
    conn.commit()
    print ("book add")

def add_member (member_name ):
    cur.execute("insert into members (name,join_date) values (%s,%s)",
(member_name ))
    conn.commit()
    print ("member add")

def borrow_book(bookid,member_id):
    cur.execute("select available from books where book_id=%s",(bookid))
    available=cur.fetchone()
    if not available or not available[0]:
        print("❌ Book is not available.")
        return
    cur.execute("insert into borrow (book_id,member_id) values (%s,%s)",
(bookid,member_id) )
    cur.execute("update  books set available='false' where book_id=%s,
(bookid) ")
    conn.commit()
    print("✅ Book borrowed.")

def book_list():
    cur.execute("select book_id ,title,author from books ")
    books =cur.fetchall()
    print("Book is available.")
    for book in books:
        print(f"Book : {book}")


if __name__ == "__main__":
#    while True:
    print("\n--- Library Menu ---")
    print("1. Add Book")
    print("2. Register Member")
    print("3. Borrow Book")
    print("4. View Available Books")
    choice = input("Enter your choice: ")
    if choice == '1':
        title = input("Enter book title: ")
        author = input("Enter author: ")
        add_book(title, author)
    elif choice == '2':
        member_name =input("Enter Memeber name")
        add_member(member_name)

    elif choice == '3':
        bookid  = input("Enter Book ID")
        borrow_book(bookid)
    elif choice == '4':
        book_list()

    conn.close()

No comments:

Post a Comment

Powered by Blogger.

Labels

Contact Form

Name

Email *

Message *

Search This Blog

Blog Archive

Ad Code

Responsive Advertisement

Recent Posts