Python: Library Managment System -P3
Learning : Python, DataBase, SQlite
Subject: Create Simple Library Managment System
In this Part we will work on Classification Managment. As we know each book can fall in one or more subject or say ‘Classification’ then we can search for a book by it’s class, example of this, we may have classifications like: cook, computer, Health, History; also a book can have more than one classifications such as one book we gave it cook and sweet .. and so-on.
To do this we add another table to the project to hold all the Classifications and manage them. Here is the code ..
# New Table to be added. sql_class = "CREATE TABLE if not exists classifi_list (class_id INTEGER PRIMARY KEY AUTOINCREMENT, class_name text )" c.execute(sql_class) db_conn.commit() c.execute ("INSERT INTO classifi_list (class_id) VALUES(:class_id)",{"class_id":0}) db_conn.commit()
[NOTE: This code been added to the source file.]
Classification Managment: In Classification Managment we will have four Functions, and will write the codes to perform each function, also we will write the Menu Function to let the user select a function. Functions are:
1. Add New Classification.
2. Edit a Classification information.
3. Delete a Classification.
4. Show Classifications.
First: Here is the class_menu() Function, the user will have the prompt and asked to select an action or (9) to Exit.
# Classification Managment Fumction def class_menu(): while True : os.system('clear') print('\n ====== LMS - Classification Managment ======') print(' 1. Add New Classification.') print(' 2. Edit a Classification information.') print(' 3. Delete a Classification.') print(' 4. Show Classifications') print(' 9. Exit.') user_choice = input('\n Select the Action you want from the Menu: ') if user_choice == '1' : # Function to Add New Classification new_classification() elif user_choice == '2' : # Function to Edit a Classification information. edit_classification() elif user_choice == '3' : # Function to Delete a Classification. delete_classification() elif user_choice == '4' : # Function to Show a Classification. show_classification() elif user_choice == '9' : return
Now, let’s start with Add New Classification. Simply we will ask the user to write a classification to be added to the Database, then we will check if it is already available in our Database, If yes then we will till the user so, or (if not available) we will add it, and will give the user the chance to add another one. If the user enter (Q or q) then we exit (Quit) from the function and return to the previous Menu.
# Function to Add New Classification def new_classification() : os.system('clear') print('\n ====== Add New Classification ======') while True : class_name = input('\n Enter the Classification and Press Enter. [ To Exit Enter Q ].. > ').capitalize() c.execute ("select * from classifi_list where class_name='{}'".format(class_name)) result = c.fetchone() if class_name not in ['q','Q']: if (result != None) : print('\n We already have [{}] in the Classification Database. '.format(class_name)) else: c.execute ("INSERT INTO classifi_list (class_name) VALUES(:class_name)",{"class_name":class_name}) db_conn.commit() print('\n One Classification Added ... ') else: input('\n To Exit ... Press any key ..') return
The Code![]() |
Code, Run-Time![]() |
Another Function to work on is Edit a Classification information In this one we will print-out all the Classifications we have and the user will select the one to Edit and will asked to Enter the ID number next to it. Then to Enter the New One and we will save it. Here is the code and the Out-put screen shot. To display all classifications on the screen we will use this code..
# Code to display the classifications on the screen # First we list down all classifications. c.execute ("select * from classifi_list where class_id > 0 order by class_name") class_list = c.fetchall() for cla in range (0,(len(class_list)-1),4): try: print('{:<3}{:<20}'.format(class_list[cla][0],class_list[cla][1]),end="") print('{:<3}{:<20}'.format(class_list[cla+1][0],class_list[cla+1][1]),end="") print('{:<3}{:<20}'.format(class_list[cla+2][0],class_list[cla+2][1]),end="") print('{:<3}{:<20}'.format(class_list[cla+3][0],class_list[cla+3][1])) except: pass
[NOTE: We use the try: except to avoid any (index out of range) errors.]
Code![]() |
Code, Run-time ![]() |
Next we will work on the Delete a Classification, Deleting may effect on some Books that uses that classification, so we need to till the user to confirm Deleting. Also we will use the same code to list down all the classifications and ask the user to enter the ID of the one to-be Deleted. Here is a part of the code to Delete a classification..
# Part of the code .. del_class = input('\n\n To Delete a Classification Enter it''s ID number [Q To Exit] > ') if del_class not in ['Q','q'] : c.execute ("select * from classifi_list where class_id = '{}' ".format(int(del_class))) c_to_del = c.fetchone()[1] print('\n Are you sure you want to Delete "{}" Classification? '.format(c_to_del)) print(' This action may effect on books has this Classification.') user_approve = input('\n If you are sure to Delete "{}" Press Y or N: > '.format(c_to_del)) if user_approve in ['y','Y'] : c.execute ("delete from classifi_list where class_id = '{}' ".format(del_class)) db_conn.commit() input('\n One Classification has been Deleted... Press any Key > ') else: input('\n\n You Select NOT to Delete the "{}" Classification, Press any key to go back.. '.format(c_to_del)) if input('\n Do you Want to Delete Another Classification? [Y,N] > ') in ['n','N'] : return else: input('\n You Select to Exit .. Press any Key > ') return
The Code![]() |
Code Run-Time![]() |
Last Function to work in this post is Show Classifications the function we will list down all the Classifications on the screen. Very easy one, here it is
# Show Classification Function def show_classification(): os.system('clear') print('\n ====== Show Classification ======') print(' The List of Classifications We Have, Sort in Alphbatic\n') c.execute ("select * from classifi_list where class_id > 0 order by class_name") class_list = c.fetchall() for cla in range (0,(len(class_list)-1),4): try: print('{:<3}{:<20}'.format(class_list[cla][0],class_list[cla][1]),end="") print('{:<3}{:<20}'.format(class_list[cla+1][0],class_list[cla+1][1]),end="") print('{:<3}{:<20}'.format(class_list[cla+2][0],class_list[cla+2][1]),end="") print('{:<3}{:<20}'.format(class_list[cla+3][0],class_list[cla+3][1])) except: # just in case error, pass and do nothing. pass input('\n\n ... Press any Key ..')
Code![]() |
Code Run-time![]() |
What’s Coming In coming post, we will write the Functions to Manage the Authors.
[ 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.
:: Library Managment System ::
Part 1 | Part 2 | Part 3 | |
To Download my Python code (.py) files Click-Here
By: Ali Radwani
-
June 19, 2020 at 3:25 pmPython: Library Managment System -P1 | Ali's Photography Space...
-
June 21, 2020 at 8:25 amPython: Library Managment System -P4 | Ali's Photography Space...
-
July 1, 2020 at 8:18 amPython: Library Managment System -P5 | Ali's Photography Space...
-
July 5, 2020 at 8:11 amPython: Library Managment System -P6 | Ali's Photography Space...