Created
December 13, 2022 05:48
-
-
Save mriamnobody/55bb4c492ecbfcd0931a33b7f2e46cf5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
import sqlite3 | |
from tabulate import tabulate | |
conn = sqlite3.connect('bookshelf.db') | |
c = conn.cursor() | |
c.execute("""CREATE TABLE IF NOT EXISTS books ( | |
title TEXT, | |
author TEXT, | |
genre TEXT, | |
location TEXT | |
)""") | |
conn.commit() | |
def add_book(): | |
while True: | |
title = input("\nEnter the Title of the book: ") | |
if title != "": | |
break | |
else: | |
print("\nPlease enter a valid title.") | |
c.execute("SELECT * FROM books WHERE title = :title", {'title': title}) | |
books = c.fetchall() | |
if books != [] : | |
print("\nBook already exists in the library\n") | |
return | |
while True: | |
author = input("\nEnter the Author of the book: ") | |
if author != "": | |
break | |
else: | |
print("\nPlease enter a valid author.") | |
while True: | |
genre = input("\nEnter the Genre of the book: ") | |
if genre != "": | |
break | |
else: | |
print("\nPlease enter a valid genre.") | |
while True: | |
location = input("\nEnter the Location of the book: ") | |
if location != "": | |
break | |
else: | |
print("\nPlease enter a valid location.") | |
c.execute("INSERT INTO books VALUES (:title, :author, :genre, :location)", | |
{ | |
'title': title, | |
'author': author, | |
'genre': genre, | |
'location': location | |
}) | |
conn.commit() | |
c.execute("SELECT * FROM books WHERE title = :title", {'title': title}) | |
books = c.fetchall() | |
print("\nBook added successfully") | |
for book in books: | |
print("\nTitle:",book[0]) | |
print("Author:",book[1]) | |
print("Genre:",book[2]) | |
print("Location:",book[3]) | |
def view_books(): | |
c.execute("SELECT * FROM books") | |
books = c.fetchall() | |
print(tabulate(books, headers=['Title', 'Author', 'Genre', 'Location'], tablefmt='grid')) | |
def delete_book(): | |
# c.execute("DELETE FROM books WHERE title IS NULL") | |
while True: | |
title = input("\nEnter the Title of the book: ") | |
if title != "": | |
break | |
else: | |
print("\nPlease enter a valid title.") | |
c.execute("SELECT * FROM books WHERE title = :title", {'title': title}) | |
books = c.fetchall() | |
#print the contents of the books list | |
if books != []: | |
c.execute("DELETE FROM books WHERE title = :title", {'title': title}) | |
print("\nBook deleted successfully\n") | |
for book in books: | |
print("\nTitle:",book[0]) | |
print("Author:",book[1]) | |
print("Genre:",book[2]) | |
print("Location:",book[3]) | |
else: | |
print("\nBook not found\n") | |
conn.commit() | |
def search_book(): | |
while True: | |
title = input("\nEnter the Title of the book: ") | |
if title != "": | |
break | |
else: | |
print("\nPlease enter a valid title.") | |
pattern = "%"+title+"%" | |
c.execute("SELECT * FROM books WHERE title LIKE ?", (pattern,)) | |
books = c.fetchall() | |
if books != []: | |
print("\nBook found in the library") | |
for book in books: | |
print("\nTitle:",book[0]) | |
print("Author:",book[1]) | |
print("Genre:",book[2]) | |
print("Location:",book[3]) | |
else: | |
print("\nBook not found in the library") | |
def update_book(): | |
while True: | |
title = input("\nEnter the Title of the book: ") | |
if title != "": | |
break | |
else: | |
print("\nPlease enter a valid title.") | |
pattern = "%"+title+"%" | |
c.execute("SELECT * FROM books WHERE title LIKE ?", (pattern,)) | |
books = c.fetchall() | |
for book in books: | |
print("\nTitle:",book[0]) | |
print("Author:",book[1]) | |
print("Genre:",book[2]) | |
print("Location:",book[3]) | |
if books != []: | |
while True: | |
print("\nWhat would you like to edit?") | |
print("1. Title") | |
print("2. Author") | |
print("3. Genre") | |
print("4. Location") | |
print("5. Cancel\n") | |
edit_choice = input("Enter your choice: ") | |
if edit_choice == "1": | |
while True: | |
new_title = input("\nEnter the new Title of the book: ") | |
if new_title != "": | |
break | |
else: | |
print("\nPlease enter a valid title.") | |
c.execute("UPDATE books SET title = 'new_title' WHERE title = title",{'title': new_title}) | |
print("\nTitle updated successfully\n") | |
conn.commit() | |
break | |
elif edit_choice == "2": | |
while True: | |
new_author = input("\nEnter the new Author of the book: ") | |
if new_author != "": | |
break | |
else: | |
print("\nPlease enter a valid author.") | |
c.execute("UPDATE books SET author = 'new_author' WHERE title = author", {'author': new_author}) | |
print("\nAuthor updated successfully\n") | |
conn.commit() | |
break | |
elif edit_choice == "3": | |
while True: | |
new_genre = input("\nEnter the new Genre of the book: ") | |
if new_genre != "": | |
break | |
else: | |
print("\nPlease enter a valid genre.") | |
c.execute("UPDATE books SET genre = 'new_genre' WHERE title = genre", {'genre': new_genre}) | |
print("\nGenre updated successfully\n") | |
conn.commit() | |
break | |
elif edit_choice == "4": | |
while True: | |
new_location = input("\nEnter the new Location of the book: ") | |
if new_location != "": | |
break | |
else: | |
print("\nPlease enter a valid location.") | |
c.execute("UPDATE books SET location = 'new_location' WHERE title = location", {'location': new_location}) | |
print("\nLocation updated successfully\n") | |
conn.commit() | |
break | |
elif edit_choice == "5": | |
break | |
else: | |
print("\nPlease enter a valid choice.\n") | |
else: | |
print("\nBook not found in the library") | |
def total_books(): | |
c.execute("SELECT COUNT(*) FROM books") | |
books = c.fetchall() | |
#count total entries in the table books under column title and print it out as an integer value | |
print("There are total books in the library: ", books[0][0]) | |
def main(): | |
while True: | |
print("\n" + " " * 10 + "Bookshelf" + " " * 10 + "\n") | |
print("1. Add a new Book") | |
print("2. View all Books") | |
print("3. Search for a Book") | |
print("4. Delete a Book") | |
print("5. Update a Book") | |
print("6. Total Books") | |
print("7. Exit\n") | |
choice = input("Enter your choice: ") | |
if choice == "1": | |
add_book() | |
elif choice == "2": | |
view_books() | |
elif choice == "3": | |
search_book() | |
elif choice == "4": | |
delete_book() | |
elif choice == "5": | |
update_book() | |
elif choice == "6": | |
total_books() | |
elif choice == "7": | |
print("\nExiting...") | |
print("Thank you for using Library Management Tool\n") | |
break | |
elif choice == "": | |
print("\nPlease enter a valid choice.") | |
else: | |
print("\nPlease enter a valid choice.") | |
main() | |
conn.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment