Home > Learning, Lesson, Problem, Projects/Experiments, Python > Python: Library Managment System -P5

Python: Library Managment System -P5

Learning : Python, DataBase, SQlite
Subject: Create Simple Library Managment System

In this Part we will work on Books Managment Functions, New Book, Edit, Delete and Show Books. First, I want to mention that I did some modifications on:

  • Authors Functions:
    Update on show_Author, Edit_author, Delete_author and new_authors.
  • Classifications Functions:
    Update on Show_classification, Edit Classification, Delete Classification.

When I start writing the code for Adding New Book after that the code for Editing a Book, I notes that both are complex and going back or calling other functions such as show_authors, show_classification, I fond that we need to do some modifications on those functions, for all that I will not cover all codes in New and Edit functions. All the code IS available in the Download Page.

So lets start here with the easy functions, First we will see the show_book, in calling this function we will pass two parameters alone, list_type as def show_books(alone=’Yes’, list_type = “D”) both has default values.
alone: alone will be ‘No’ if we call the function within another function.
list_type: will have ‘D’ for Detail list of Book, list with Author data and Classifications. ‘T’ for Books list ID and Title only.
Here is the code ..

 # show books with two parameters

def show_books(alone='Yes', list_type = "D") : 
    c.execute ("select * from books where b_id > 0  order by b_name")     
    books_list = c.fetchall()           
    if list_type =="D":
        print('\n   ====== Show Books ======')
        print('\n   The List of Books We Have, Sorted by Book Title\n') 
        for book in range (0,(len(books_list))):
               print('\n             ID: {}'.format(books_list[book][0])) 
               print('     Name/Title: {}'.format(books_list[book][1]))
               print('      Book ISBN: {}'.format(books_list[book][3]))
               print('   Publish Date: {}'.format(books_list[book][4])) 
               print('   Book Edition: {}'.format(books_list[book][5]))
               # To get the Book Classification. 
               c.execute ("select class_name from classifi_list INNER JOIN b_class ON (classifi_list.class_l_id = b_class.class_id) AND (b_class.b_id ={})".format(books_list[book][0]))     
               b_class_list = c.fetchall()
               print('\n   Book Classification:')
                   for i in range(0,len(b_class_list),5) :
                       print(' '*15,b_class_list[i][0], end ="")
                       print(' | ',b_class_list[i+1][0], end ="")
                       print(' | ',b_class_list[i+2][0], end ="")
                       print(' | ',b_class_list[i+3][0])    
               auth_id = books_list[book][2]
               # To get the Author Data. 
               c.execute ("select * from authors where a_id = {}".format(int(auth_id)))     
               auth_list = c.fetchone()
               # To get the Author's Social Media Accounts. 
               c.execute ("select * from sma where a_id = {}".format(auth_id))  
               sma_list = c.fetchall()  
               # To print-out Author Data.
               print('\n\n   The Book Author:')
               print('           Name: {}'.format(auth_list[1]))
               print('          Email: {}'.format(auth_list[2]))
               print('          Social Media:')
               # Here we list the SMA for the Author.
               for each in sma_list :  
                  print('{:<14}{:<13} [ {} ]'.format('',each[2],each[3]))
        input('\n\n         ... Press any Key ..') 

    if list_type == "T" :
        for book in range (0,(len(books_list)),4):
                print('    {:<3}{:<27}'.format(books_list[book][0],books_list[book][1]),end="")
            #Exception for index out of range error.
        if alone =='Yes' :         
            input('\n\n         ... Press any Key ..')
        else : return    

Now the Delete Function, this is easy one, we just print print-out all the Book and the user will Select the one to be delete by Entaring it’s ID. To list the Books we will call the show_book function as here: show_books(‘No’,’T’) so parameter alone=’No’ and parameter list_type =’T’. Here is the code ..

 # Function to Delete a Book

def delete_book () :

    print('\n   ====== Delete a Book ======')
    print('\n   The List of Books We Have, Sorted by Book Title\n') 
    # First we show all Bookd so the user can select the One to be Deleted.
    # we Call show_books function and pass 'T'. 
    del_book = input('\n\n   Entert the Book ID that you want to Delete: [Q to Exit]  >  ') 
    if del_book not in ['q','Q'] :
            c.execute ("select b_name from books where b_id = {}".format(int(del_book)))     
            d_b_name = c.fetchone() 
            if input('\n   Are you Sure you want to Delete Book "{}"? [Y,N].  >   '.format(d_b_name[0])) in ['Y','y'] :             
                c.execute ("delete from books where b_id = {} ".format(int(del_book)))
                input('\n   One Book has been Deleted... Press any Key > ')    
                print('\n   You Select NOT To Delete The Book "{}" . '.format(d_b_name[0]))
                input('\n   To Exit ... Press any key ..') 
            input('\n   Not valid .. ')

Now lit’s start with Adding New Book to the Library System. In this process we will Enter New Book Name/Title, Book ISBN, Book Publish Date, Book Edition, and select an Author from the Authors List and if the Author is not exist we can Enter New Author, also to select a Classification and if we want we can Enter a new Classification.[If the one we want is not in the list]. As we can see in the Adding New Book we will call other functions to Show Authors then to Add new Author [If the one we have not on the list] also we will call the Show Classifications to select from and we may need to Add New classification.

The Editing the Books was really challenging, Showing all the the Books on the screen so the user will select the one to be edited, then once finished editing the Book attributes we will ask if the user want to edit the Author information if (YES) again will go through all Author attribute and ask if each one need to be Edit, IF the Author is not in the Authors list we will take the user to Add new Author to the system (if want to), after that we will ask if the user want to edit the classifications of the Book and if the classificatio is not in the list we will call the new_classification function.
Here are screen shot of the functions. ..

Part of Add New Book..
Part of Editing the Book.

What’s Next:
1. I will go through the code Run-Time tring to fix any errors.
2. I will write function for simple statistics on the Library.

[ NOTE ]
1. I am using Galaxy Tab and QPython3 App.
2. All the above codes are available in the Download Section/Page under the project name.
3. The application codes, Functions, Menus and other parts of the Application are subject of changes. In case of changes I will mention that.
4. This is a simple personal Library application, so i did not use any validations on Data Entry.

:: Library Managment System ::

Part 1 Part 2 Part 3 Part 4
Part 5

To Download my Python code (.py) files Click-Here

Follow me on Twitter..

By: Ali Radwani

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 )

Facebook photo

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

Connecting to %s