-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlibrary.py
More file actions
71 lines (59 loc) · 2.27 KB
/
library.py
File metadata and controls
71 lines (59 loc) · 2.27 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import sqlite3
from book import Book
from database import Database
class Library:
def __init__(self):
self.db = Database()
self.books = self.load_books()
def add_book(self, book):
try:
self.db.connect()
self.db.cursor.execute("INSERT INTO books (isbn, title, author) VALUES (?, ?, ?)",
(book.isbn, book.title, book.author))
self.db.conn.commit()
print(f"'{book.title}' kütüphaneye eklendi.")
except sqlite3.IntegrityError:
print(f"Hata: '{book.title}' (ISBN: {book.isbn}) zaten kütüphanede mevcut.")
finally:
self.db.close()
self.books = self.load_books()
def remove_book(self, isbn):
self.db.connect()
self.db.cursor.execute("DELETE FROM books WHERE isbn = ?", (isbn,))
self.db.conn.commit()
self.db.close()
print(f"ISBN numarası '{isbn}' olan kitap silindi.")
self.books = self.load_books()
def list_books(self):
self.books = self.load_books()
if not self.books:
print("Kütüphanede henüz kitap bulunmuyor.")
return
print("\n--- Kütüphanedeki Kitaplar ---")
for book in self.books:
print(book)
print("-----------------------------\n")
def find_book(self, isbn):
self.db.connect()
self.db.cursor.execute("SELECT title, author, isbn FROM books WHERE isbn = ?", (isbn,))
row = self.db.cursor.fetchone()
self.db.close()
if row:
return Book(row[0], row[1], row[2])
return None
def load_books(self):
self.db.connect()
self.db.cursor.execute("SELECT title, author, isbn FROM books")
rows = self.db.cursor.fetchall()
self.db.close()
return [Book(row[0], row[1], row[2]) for row in rows]
def update_book(self, isbn, title, author):
self.db.connect()
self.db.cursor.execute(
"UPDATE books SET title = ?, author = ? WHERE isbn = ?",
(title, author, isbn)
)
self.db.conn.commit()
self.db.close()
self.books = self.load_books()
print(f"ISBN numarası '{isbn}' olan kitap güncellendi.")