def get_book(self, isbn: str): book = Book() with self.db_conn as connect: with connect.cursor() as cur: cmd = """ SELECT * FROM book_table WHERE isbn=%s """ cur.execute(cmd, (isbn, )) row = cur.fetchone() book.isbn = row[0] book.title = row[1] book.publisher = row[2] book.author = row[3] book.pubdate = row[4] book.pages = row[5] book.desc = row[6] book.image = row[7] return book
def load_books_from_db(self): """ Load all the books in the bookshelf into the list :return: """ with self.conn as conn: with conn.cursor() as curs: cmd = "SELECT * FROM book_table ORDER BY isbn;" curs.execute(cmd) rows = curs.fetchall() for row in rows: book = Book() book.isbn = row[0] book.title = row[1] book.publisher = row[2] book.author = row[3] book.pubdate = row[4] book.pages = row[5] book.desc = row[6] book.image = row[7] self.book_list.append(book)
def update_images(bs: BookShelf, to: int =5): import time index = 1; booklist = list() bs.get_books_to_list(booklist) for which_book in booklist: book_copy = Book(which_book.isbn) print("{} {}".format(index, book_copy)) bs.update_book(book_copy) time.sleep(to) index += 1
def main(): bs = BookShelf() if bs.open(): booklist = [] bs.get_books_to_list(booklist, sortby=SortBy.TITLE) print("The bookshelf contains {} books".format(len(booklist))) for key in Book().get_keys(): print("Key: {}".format(key)) # book_copy = Book(booklist[0].isbn) # print(book_copy) # bs.update_book(book_copy) bs.close()
def handle_scanned_code(self, isbn: str): if self.is_isbn_in_list(isbn): self.display_product_rescan() else: label = "Last ISBN # Scanned : {:14}".format(isbn) self.center_label(self.scr, 6, label) book = Book(isbn) if book: self.draw_book(book) self.add_book_to_db(book) self.book_list.append(book) self.last_scanned_code = isbn else: self.display_not_isbn_no() self.redraw_border() self.redraw_stats_subwin()
def test(): import time test_isbn = "9781571104052" bs = BookShelf() if bs.open(): booklist = [] bs.get_books_to_list(booklist) for which_book in booklist: print(which_book) print("=" * 128) book_obj = Book(test_isbn) print(book_obj) bs.add_book(book_obj) time.sleep(10) # Give user time to check that the database was altered bs.del_book(book_obj) bs.close()
def get_books_to_list(self, l: list, sortby=None): logging.getLogger("bookshelf").debug("get_books_to_list()") with self.db_conn as conn: with conn.cursor() as cur: by_field = "isbn" if sortby is not None: book_fields = list(Book().get_keys()) by_field = book_fields[int(sortby) - 1] cmd = "SELECT * FROM book_table ORDER BY {}".format(by_field) cur.execute(cmd) rows = cur.fetchall() for row in rows: book = Book() book.isbn = row[0] book.title = row[1] book.publisher = row[2] book.author = row[3] book.pubdate = row[4] book.pages = row[5] book.desc = row[6] book.image = row[7] l.append(book) return True