Skip to content

PYTHON Simple Library System

library.py

import sqlite3
import time

class Book():
  def __init__(self,name,author,publisher,bookType,printing):
    self.name = name
    self.author = author
    self.publisher = publisher
    self.bookType = bookType
    self.printing = printing
  
  def __str__(self):
    return "Book name:{}\nAuthor:{}\nPublisher:{}\nbookType:{}\nPrinting:{}\n".format(self.name,self.author,self.publisher,self.bookType,self.printing)

    

class Library():
  def __init__(self):
    self.create_link()

  def create_link(self):
    self.link = sqlite3.connect("library.db")
    self.cursor = self.link.cursor()
    query = "CREATE TABLE IF NOT EXISTS books (name TEXT,author TEXT,publisher TEXT,bookType TEXT,printing INT)"
    self.cursor.execute(query)
    self.link.commit()

  def link_close(self):
    self.link.close()

  def books_show(self):
    query = "SELECT * FROM books"
    self.cursor.execute(query)
    books = self.cursor.fetchall()

    if(len(books) == 0):
      print("There are no books in the library.")
    else:
      for i in books:
        book = Book(i[0],i[1],i[2],i[3],i[4]) # name, author, publisher, bookType, printing
        print(book)

  def book_query(self,name):
    query = "SELECT * FROM books WHERE name = ?"
    self.cursor.execute(query,)
    books = self.cursor.fetchall()
    if(len(books) == 0):
      print("There is no such book.")
    else:
      book = Book(books[0][0],books[0][1],books[0][2],books[0][3],books[0][4])
      print(book)
        
  def add_book(self,book):
    query = "INSERT INTO books VALUES(?,?,?,?,?)"
    self.cursor.execute(query,(book.name,book.author,book.publisher,book.bookType,book.printing))
    self.link.commit()

  def delete_book(self,name):
    query = 'DELETE FROM books WHERE name = ?'
    self.cursor.execute(query,(name,))
    self.link.commit()

  def update_printing(self,name):
    query = 'SELECT * FROM books WHERE name = ?'
    self.cursor.execute(query,(name,))
    books = self.cursor.fetchall()
    if(len(books) == 0):
      print("There is no such book.")
    else:
      printing = books[0][4] # printing
      printing += 1
      query2 = 'UPDATE books SET printing = ? WHERE name = ?'
      self.cursor.execute(query2,(printing,name))
      self.link.commit()

library_execute.py

from library import *

print("""

***********************************************
Welcome python library system
transactions:
1. Show books
2. Query book
3. Add book
4. Delete book
5. Upgrade Printing

Press q to exit...
***********************************************

""")

library = Library() 

while True:
  operation = input('Your action: ')

  if(operation == 'q'):
    print('Program ended')
    break

  elif(operation == '1'):
    library.books_show()
    
  elif(operation == '2'):
    name = input('name: ')
    time.sleep(2)
    library.book_query(name)
    
  elif(operation == '3'):
    name      = input('name: ')
    author    = input('author: ')
    publisher = input('publisher: ')
    bookType  = input('bookType: ')
    printing  = input('printing: ')
    time.sleep(2)
    newBook = Book(name,author,publisher,bookType,printing)
    library.add_book(newBook)
    print('Book added')
    
  elif(operation == '4'):
    name = input('name: ')
    time.sleep(2)
    library.delete_book(name)
    print('Book deleted')
    
  elif(operation == '5'):
    name = input('name: ')
    library.update_printing(name)
    print('Printing upgraded')


  
  else:
    print('Invalid action')

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: