Bookstore Management System

Python and MySQL combined Project on Bookstore Management

In this project we are going to see how we can create a Bookstore Management System using Python and MySQL.

A Bookstore Management System is a computer-based system that is used to manage and keep track of the inventory, orders, and sales of a bookstore. It is typically used by bookstores that maintain a large inventory of books and other related items.

Scenario

This program will be directly used by the store owner, where he will be manually adding the book details and whichever customer is coming for purchasing the book, the owner will be adding customer details along with the book's ISBN Number which the customer purchased. This is the whole scenario of this project.

In this project we will see a console based application through which we can store books, take orders from customers, store customer details and calculate total sales. Other than this, we will be able to view all the books details, update and delete the books, alongside, we can view all the customers as well.

In addition to this a login credentials for admin has been provided but there is no option for user to be created it is being directly maintained by the Database Administrator.

So here is a list of tasks we will perform in the above projects.

Installation of MySQL Connector

First Step, Install the MySQL connector you can install it using either

  1. Right clicking on the windows icon and select windows powershell and on windows powershell prompt. Type--->

    pip install mysql-connector-python It will be installed directly into the site packages folder of your python and due to this you will be able to use it anywhere in any IDE using the python installed in your windows.

  2. Or using PyCharm follow these steps to install the mysql-connector-python

    1. Click on File

    2. Click on settings

    3. Click on Project --> Python Interpreter

    4. Click on + button on left side

    5. On Search Bar Type mysql-connector-python

    6. Install it

See the Images

Importing the Module mysql.connector

Connecting to MySQL using MySQL Credentials

Here, in host if you are using mysql in your local machine provide localhost as the host and in user and password provide your MySQL username and password to login into the MySQL and in database provide the database in which you want to work. This may be of different name for different people. (Before doing this, ensure your MySQL is working either through MySQL workbench, xampp or wamp).

Creation of Database and Tables in MySQL

1.create database book_store 2. use book_store 3. create table login_book(userid varchar(20) primary key, password varchar(20) unique) 4. create table books(isbn bigint primary key, title varchar(100), author varchar(100), publisher varchar(100), price int, quantity_sold int, total_sales int) 5. create table cust_purchase(cust_id int primary key auto_increment, cname varchar(30), cphone bigint(10), isbn bigint, foreign key(isbn) references books(isbn))

Tables and Databases used in the project

There are two tables used : 1. login_book for login into the system 2. books for storing the book details. 3. cust_purchase for storing the customer details.

Start of the program

The program starts from a main() function asking the user for the credentials to verify and log into the system. The function written below is main() and it takes userid and password as input from the user(This userid and password input from user is totally different from what we created in above table). Then these credentials are verified and if user id is wrong then message for userid is shown while if password is wrong message for wrong password is shown to the user. Here is the function,

The SQL Query used for validating the userid is SELECT * FROM LOGIN_BOOK The module mysql.connector contains a function execute(query) where we have to pass our query and this function will execute your query in the database. This function can be accessed using the reference of cursor such as cur here is used to execute the query. Inside this function you can write your query in two ways - 1. query = "select * from login_book" cur.execute(query)

Using above method we can create our query in a string and then we can pass that string of query to the execute function. 2. cur.execute("select * from login_book") You can directly pass your query also. So this main() function will validate your identity and if it is correct then it will call the method start(userid). start(userid) function is the point where all the system starts, here userid is a parameter passed in which the userid you entered which is validated with the database is sent to show in case of log out from the system. Now we define start(userid) function, here is the code:

Prints a menu to the user asking to enter the choice of operation he want to perform. Sometimes user may enter wrong choice i.e. other than 1 to 8 then a message to display is added in else block. On choosing option 8 the elif block contains a break statement which will break the loop and exit the program by showing the userid of the user.

Here is the images,

main() function starts here

Addition of Books (Choice 1)

On choosing the option 1, the program is redirected to function add_book() to add books into the book store. In this add_book() function Book ISBN, title, author, publisher and price is entered into the database. Here is the code,

Let's have a look into the code above and try to understand -

total_sales variable is created using global keyword because we want to access this keyword everywhere in the program. (using global keyword we can either access the global variable of the program or can create a variable that will be declared globally and will be available to all the functions.) total_sales variable is for storing the total sales per day and which will be calculated using - total_sales = quantity_sold * price This total_sales will be stored into the database of book_store in books table when a customer is added for any book(in case of purchase).

Now moving furthur....

Here we will take input of isbn number from user and will be check on the basis of this and will store into the database. The ISBN number is taken as a string input because we want to check the length of the isbn number so instead of writing another code for obtaining the number of digits we can simply use strings.

Since we want that isbn number must be of len(isbn) = 5 and will check whether the user has provided the correct ISBN number of not without '-' . Below, we have taken the neccessary inputs from the user to store into the databases.

Now converting the isbn into an integer value and Writing Query for inserting the record into the database.

And all the details will be inserted into the MySQL database successfully.

Updating the Book Details(price only)

We will update the price and quantity sold details in a book using isbn number. Through these details total_sales will be calculated.

Here is the code for update_book(),

Let's have a look into the code and try to understand,

ISBN number is asked to find the record on which update is to be done.

Now Query of MySQL is executed using cur.execute(query) function to fetch the record of ISBN Number provided.

In the next step, we check for whether the book with that isbn number is there in database or not. When a book with the ISBN number provided is not in the books table then an empty list will be returned with no records. Using this, we can check for whether we have any book or not.

Now price is asked from user to update the database. Simple UPDATE Query of MySQL is used to update the database.

Deletion of Book using it's ISBN Number

If we want to delete our record from our book store then this can be achieved using delete query of MySQL here is the code to achieve this,

Taking input of ISBN Number to find the record with the matching ISBN Numbered book to delete.

This record will be a list of tuple and therefore is stored into the list for further operation. Here try and except block is used because we have a relation of cust_purchase table with books table using isbn number through Foreign Key, so there may occur delete anomaly while deleting a book record from database as a customer may exist with that book purchased.

Searching of Book on the basis of different conditions

We can perform a search on the basis of different factors such as ISBN Number, Book title, Author Name and Publisher Name. But it is more preferred to search as per the ISBN Number since records fetched will be unique always in case of searching through ISBN Number.

Here is the code for search_book(),

Above is a very simple Code, using which we can search a book by providing either ISBN number or other things such as author name and all. Like main() for each choice there is an if - elif block. query = "select * from books where isbn = search_value" Where search_value is the value which is used for searching the value, it may be author name or publisher name depending upon on which choice user want to search.

Viewing all the books

All the books can be viewed using the function view_books() here is the code,

All the records can be fetched using select * from books

Then all the records can be printed in tabular form using some string formatting functions and for loop.

Addition of Customer Details on the time of Book Purchase

While a customer purchase a book, the bookstore owner will add the record of that customer into his database and on the basis of this addition the quantity_sold and total sales in books table will be updated. Here is the code to achieve this,

Let's try to understand each block of code,

In this code, we display all the books to the operator for his help to add customer details.

Now appropriate details of customer has been taken and query for adding into the database is written. On closer look into the insert query we have not added the cust_id column declared during the creation of table, it is due to this column is set to auto_increment and primary key, we don't need to provide any value, it will automatically asign value 1 to the first column and will simply increment on insertion of every next record.

While executing the query, try and except block is used to handle the error when there is no book with the isbn provided due to integrity imposed by foreign key. In this Query,

We have used join and subquery both to update the quantity_sold in books table because we want that whenver a customer is added, the system must automatically update the record in the books table such as quantity_sold and total_sales.

Above are the images for customer addition and record updation in books table.

Full Source Code

In this way the project involves the knowledge of both the Python and MySQL.

Last updated