-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlibrary.py
More file actions
55 lines (48 loc) · 1.62 KB
/
library.py
File metadata and controls
55 lines (48 loc) · 1.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import mysql.connector
conn = mysql.connector.connect(
host="localhost",
user="root",
password="A_ta25!wbY66@",
database="mydb"
)
cursor = conn.cursor()
# --- JOIN: See who borrowed which book and who wrote it ---
def show_borrowings():
query = """
SELECT books.title, authors.name, members.member_name, borrowings.borrow_date
FROM borrowings
INNER JOIN books ON borrowings.book_id = books.id
INNER JOIN authors ON books.author_id = authors.id
INNER JOIN members ON borrowings.member_id = members.id
"""
cursor.execute(query)
results = cursor.fetchall()
print("\n📚 Library Borrowings:")
print("-" * 55)
for row in results:1
print(f"📖 {row[0]} | ✍️ {row[1]} | 👤 {row[2]} | 📅 {row[3]}")
print("-" * 55)
# --- ADD a new borrowing ---
def borrow_book(book_id, member_id, date):
sql = "INSERT INTO borrowings (book_id, member_id, borrow_date) VALUES (%s, %s, %s)"
cursor.execute(sql, (book_id, member_id, date))
conn.commit()
print(f"✅ Book ID {book_id} borrowed by Member ID {member_id}!")
# --- MENU ---
while True:
print("\n📚 Library System")
print("1. Show all borrowings")
print("2. Borrow a book")
print("3. Quit")
choice = input("Choose an option: ")
if choice == "1":
show_borrowings()
elif choice == "2":
b = input("Book ID (1=Harry Potter, 2=Animal Farm): ")
m = input("Member ID (1=Violet, 2=Alice): ")
d = input("Date (YYYY-MM-DD): ")
borrow_book(b, m, d)
elif choice == "3":
print("Goodbye!")
conn.close()
break