{"id":1912,"date":"2022-01-25T23:23:39","date_gmt":"2022-01-25T17:53:39","guid":{"rendered":"https:\/\/cbsepython.in\/?p=1912"},"modified":"2023-12-28T10:04:39","modified_gmt":"2023-12-28T04:34:39","slug":"library-management-system-python-project-for-class-12","status":"publish","type":"post","link":"https:\/\/cbsepython.in\/library-management-system-python-project-for-class-12\/","title":{"rendered":"Library Management System Python Project for Class 12"},"content":{"rendered":"<h2><span style=\"color: #000000;\">Library Management System Python Project for Class 12<\/span><\/h2>\n<p><span style=\"color: #000000;\"># Python Project Library Management System us MySql Connectivity.\u00a0<\/span><\/p>\n<p>&nbsp;<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">import mysql.connector as sqlctr\r\nimport sys\r\nfrom datetime import datetime\r\nmycon = sqlctr.connect(host='localhost', user='root', password='admin')\r\nif mycon.is_connected():\r\n    print('\\n')\r\n    print('Successfully connected to localhost')\r\nelse:\r\n    print('Error while connecting to localhost')\r\ncursor = mycon.cursor()\r\n\r\n#creating database\r\ncursor.execute(\"create database if not exists pathsala\")\r\ncursor.execute(\"use pathsala\")\r\n\r\n#creating the tables we need\r\n\r\ncursor.execute(\"create table if not exists books(SN int(5) primary key,Book_Name varchar(30), Quantity_Available int(10),Price_Per_Day int(10))\")\r\ncursor.execute(\"create table if not exists BORROWER(SN int(5),borrowers_name varchar(40),book_lent varchar(20),contact_no int(10))\")\r\n\r\ndef command(st):\r\n    cursor.execute(st)\r\n\r\n\r\ndef fetch():\r\n    data = cursor.fetchall()\r\n    for i in data:\r\n        print(i)\r\n\r\n\r\ndef all_data(tname):\r\n    li = []\r\n    st = 'desc '+tname\r\n    command(st)\r\n    data = cursor.fetchall()\r\n    for i in data:\r\n        li.append(i[0])\r\n    st = 'select * from '+tname\r\n    command(st)\r\n    print('\\n')\r\n    print('-------ALL_DATA_FROM_TABLE_'+tname+'_ARE-------\\n')\r\n    print(tuple(li))\r\n    fetch()\r\n\r\ndef detail_burrower(name,contact):\r\n    tup=('SN','borrowers_name','book_lent','date','contact_no')\r\n    print('\\n---Details for borrower '+name+'---\\n')\r\n    print(tup)\r\n    st='select * from borrower where borrowers_name like \"{}\" and contact_no={}'.format(name,contact)\r\n    command(st)\r\n    fetch()\r\n\r\n\r\ndef days_between(d1, d2):\r\n    d1 = datetime.strptime(d1, \"%Y-%m-%d\")\r\n    d2 = datetime.strptime(d2, \"%Y-%m-%d\")\r\n    global days\r\n    days=abs((d2 - d1).days)\r\n\r\n\r\ndef price_book(days,book_name):\r\n    st1 = 'select Price_Per_Day from books where Book_Name=\"{}\"'.format(book_name)\r\n    command(st1)\r\n    data = cursor.fetchall()\r\n    for i in data:\r\n        global t_price\r\n        t_price=int(i[0])*days\r\n        print('No. of days {} book is kept : {}'.format(book_name,days))\r\n        print('Price per day for book {} is Rs.{}'.format(book_name,i[0]))\r\n        print('Total fare for book '+book_name +'-',t_price)\r\n\r\ndef lend():\r\n    flag='True'\r\n    while flag=='True':\r\n        print('\\n___AVAILABLE BOOKS___\\n')\r\n        st0 = 'select Book_Name from books where Quantity_Available&gt;=1'\r\n        command(st0)\r\n        fetch()\r\n        st1='select max(SN) from borrower'\r\n        command(st1)\r\n        data_sn=cursor.fetchall()\r\n        for i in data_sn:\r\n            SN=i[0]+1        \r\n        book_selected=str(input('Enter name of book from above list : '))\r\n        borrowers_name=str(input('Enter Borrower Name : '))\r\n        date=str(input('Enter date (YYYY-MM-DD) : '))\r\n        contact=int(input('Enter contact no. : '))\r\n        st_insert='insert into borrower values({},\"{}\",\"{}\",\"{}\",{})'.format(SN,borrowers_name,book_selected,date,contact)\r\n        command(st_insert)\r\n        st_quantity='select quantity_available from books where book_name=\"{}\"'.format(book_selected)\r\n        command(st_quantity)\r\n        data_quantity=cursor.fetchall()\r\n        for quantity in data_quantity:\r\n            qty=quantity[0]-1\r\n        st_dec='update books set quantity_available={} where book_name=\"{}\"'.format(qty,book_selected)\r\n        command(st_dec)\r\n        dec=str(input('Do you want to add more records (Y\/N) : '))\r\n        if dec.upper==\"Y\":\r\n            flag= 'True'\r\n        else:\r\n            flag='False'\r\n        \r\n\r\ndef borrowers():\r\n    print('\\n\\n___OPTIONS AVAILABLE___\\n\\nEnter 1 : To Show detail of all borrowers \\nEnter 2 : To check detail of a particular borrower \\nEnter 3 : To calculate total fine of a borrower \\nEnter 4 : To go Back \\nEnter 5 : To commit all the changes and exit')\r\n    dec = input('enter your choice-')\r\n    if dec=='1':\r\n        all_data('borrower')\r\n    elif dec=='2':\r\n        name = str(input('\\nenter borrower name-'))\r\n        contact = str(input('enter borrower contact no.-'))\r\n        detail_burrower(name,contact)\r\n    elif dec=='3':\r\n        tfine()\r\n    elif dec=='4':\r\n        action_list()\r\n    elif dec=='5':\r\n        close()\r\n    borrowers()\r\n\r\ndef tfine():\r\n    name=str(input('\\nEnter borrower name : '))\r\n    contact=input('Enter borrower contact_no : ')        \r\n    detail_burrower(name, contact)\r\n    st1 = 'select book_lent from borrower where borrowers_name =\"{}\" and contact_no={}'.format(name,contact)\r\n    command(st1)\r\n    data=cursor.fetchall()\r\n    for i in data:\r\n        book_name=i[0]\r\n        st2 = 'select date from borrower where borrowers_name=\"{}\" and book_lent=\"{}\"'.format(name,book_name)\r\n        command(st2)\r\n        data1=cursor.fetchall()\r\n        for date in data1:\r\n            date_taken=date[0]\r\n            date_return = str(input('\\nEnter returning date for book \"{}\" (YYYY-MM-DD) , Press ENTER to skip-'.format(book_name)))\r\n            while date_return!='':\r\n                days_between(str(date_return),str(date_taken))\r\n                price_book(days,i[0])\r\n                print('\\nEnter Y : If Rs.{} is paid and book is returned.\\nEnter N : If fare is not paid and book is not returned.'.format(t_price))\r\n                dec=str(input('Enter (Y?N) : ')) \r\n                if dec.upper()==\"Y\":\r\n                    st= 'select SN , Quantity_Available from books where Book_Name =\"{}\"'.format(i[0])\r\n                    command(st)\r\n                    data2=cursor.fetchall()\r\n                    for price in data2:\r\n                        update('books', 'Quantity_Available',price[1]+1,price[0])\r\n                    st_del = 'delete from borrower where borrowers_name=\"{}\" and book_lent=\"{}\"'.format(name,book_name)\r\n                    command(st_del)\r\n                    break\r\n                else:\r\n                    print(\"\\n\\nPLEASE PAY THE FARE AND RETURN BOOK AFTER READING.\\n\\n\")\r\n                    break\r\n        \r\n\r\ndef insert():\r\n    flag = 'true'\r\n    while flag=='true':\r\n        licol=[]\r\n        li1=[]\r\n        li_val=[]\r\n        command('desc books')\r\n        data=cursor.fetchall()\r\n        for i in data:\r\n            licol.append(i[0])   \r\n        command('select max(SN) from books')\r\n        dta=cursor.fetchall()\r\n        for j in dta:\r\n            li_val.append(j[0]+1)\r\n        for k in range(1,4):\r\n            val = str(input('Enter '+licol[k]+'-'))\r\n            li_val.append(val)\r\n        li1.append(tuple(li_val))\r\n        values = ', '.join(map(str, li1))\r\n        st1 = \"INSERT INTO books VALUES {}\".format(values)\r\n        command(st1)\r\n        all_data('books')\r\n        print('\\n')\r\n        print(\"\\nDATA INSERTED SUCCESSFULLY\\n\")\r\n        dec = str(input('Do u want to insert more data?(Y\/N)-'))\r\n        if dec.upper() == \"Y\":\r\n            flag='true'\r\n        else:\r\n            flag='false' \r\n    action_list()\r\n\r\ndef update(tname,col1,post_value,pre_value):\r\n    st = str('update %s set %s=%s where SN=%s') % (tname, col1, \"'%s'\", \"'%s'\") % (post_value, pre_value)\r\n    command(st)\r\n    all_data(tname)\r\n    print('\\nVALUE UPDATED SUCCESSFULLY')\r\n     \r\n\r\ndef close():\r\n    mycon.commit()\r\n    mycon.close()\r\n    if mycon.is_connected():\r\n        print('still connected to localhost')\r\n    else:\r\n        print('\\n\\nconnection closed successfully.')\r\n    sys.exit()\r\n\r\n\r\ndef action_list():\r\n    print('\\n')\r\n    print('#### WELCOME TO LIBRARY MANAGEMENT SYSTEM ####\\n\\nEnter 1 : To View details of all available Books\\nEnter 2 : To check detail of a particular book\\nEnter 3 : To lend a book \\nEnter 4 : To add new books in list \\nEnter 5 : To update data \\nEnter 6 : To view details of borrowers \\nEnter 7 : To commit all changes and exit')\r\n    dec = input('\\nenter your choice-')\r\n    if dec == '1':\r\n        all_data('books')\r\n    elif dec=='2':\r\n        tup=('SN','Book_Name','Quantity_Available','Price_Per_Day')\r\n        tup1 = ('SN', 'borrowers_name', 'book_lent', 'contact_no')\r\n        in1=str(input('enter first name , last name or middle name of a book-'))\r\n        print('\\n___ALL DATA OF BOOKS HAVING \"{}\" IN THEIR NAME FROM BOTH TABLE____'.format(in1))\r\n        st =str('select * from books where book_name like \"{}\"'.format('%'+in1+'%'))\r\n        st1=str('select * from borrower where book_lent like \"{}\"'.format('%'+in1+'%'))\r\n        print('\\n__DATA FROM TABLE BOOKS__\\n')\r\n        command(st)\r\n        print(tup)\r\n        fetch()\r\n        print('\\n__DATA FROM TABLE BORROWER__\\n')\r\n        command(st1)\r\n        print(tup1)\r\n        fetch()\r\n        print()\r\n    elif dec == '3':\r\n        lend()\r\n    elif dec=='4':\r\n        insert()\r\n    elif dec=='5':\r\n        flag='true'\r\n        while flag=='true':\r\n            tname = 'books'\r\n            li = []\r\n            st1 = 'desc '+tname\r\n            command(st1)\r\n            data = cursor.fetchall()\r\n            for i in data:\r\n                li.append(i[0])\r\n            all_data(tname)\r\n            print('\\n columns in table '+tname+' are')\r\n            print(li)\r\n            col1 = str(input('enter column name for modification from above list-'))\r\n            lipo = ['SN']\r\n            lipo.append(col1)\r\n            print(tuple(lipo))\r\n            st0 = 'select SN , %s from books' % (col1)\r\n            command(st0)\r\n            fetch()\r\n            pre_value = str(input('enter corresponding SN for the data to be changed-'))\r\n            post_value = str(input('enter new value for column %s having SN %s-' % (col1, pre_value)))\r\n            update(tname, col1, post_value, pre_value)\r\n            dec = str(input('Do you want to change more data?(Y\/N)-'))\r\n            if dec == 'y' or dec == 'Y':\r\n                flag='true'            \r\n            else:\r\n                flag='false'\r\n        \r\n    elif dec=='6':\r\n        borrowers()\r\n    elif dec=='7':\r\n        close()\r\n    action_list()\r\n\r\n\r\naction_list()\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>Output:<\/p>\n<p>&nbsp;<\/p>\n<pre>Successfully connected to localhost\r\n\r\n\r\n#### WELCOME TO LIBRARY MANAGEMENT SYSTEM ####\r\n\r\nEnter 1 : To View details of all available Books\r\nEnter 2 : To check detail of a particular book\r\nEnter 3 : To lend a book \r\nEnter 4 : To add new books in list \r\nEnter 5 : To update data \r\nEnter 6 : To view details of borrowers \r\nEnter 7 : To commit all changes and exit\r\n\r\nenter your choice-1\r\n\r\n\r\n-------ALL_DATA_FROM_TABLE_books_ARE-------\r\n\r\n('SN', 'Book_Name', 'Quantity_Available', 'Price_Per_Day')\r\n\r\n\r\n#### WELCOME TO LIBRARY MANAGEMENT SYSTEM ####\r\n\r\nEnter 1 : To View details of all available Books\r\nEnter 2 : To check detail of a particular book\r\nEnter 3 : To lend a book \r\nEnter 4 : To add new books in list \r\nEnter 5 : To update data \r\nEnter 6 : To view details of borrowers \r\nEnter 7 : To commit all changes and exit\r\n\r\nenter your choice-2\r\nenter first name , last name or middle name of a book-python\r\n\r\n___ALL DATA OF BOOKS HAVING \"python\" IN THEIR NAME FROM BOTH TABLE____\r\n\r\n__DATA FROM TABLE BOOKS__\r\n\r\n('SN', 'Book_Name', 'Quantity_Available', 'Price_Per_Day')\r\n\r\n__DATA FROM TABLE BORROWER__\r\n\r\n('SN', 'borrowers_name', 'book_lent', 'contact_no')\r\n\r\n\r\n\r\n\r\n#### WELCOME TO LIBRARY MANAGEMENT SYSTEM ####\r\n\r\nEnter 1 : To View details of all available Books\r\nEnter 2 : To check detail of a particular book\r\nEnter 3 : To lend a book \r\nEnter 4 : To add new books in list \r\nEnter 5 : To update data \r\nEnter 6 : To view details of borrowers \r\nEnter 7 : To commit all changes and exit\r\n\r\nenter your choice-3\r\n\r\n___AVAILABLE BOOKS___<\/pre>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<h2><span style=\"color: #000000;\">Explanation:<\/span><\/h2>\n<p><span style=\"color: #000000;\">This code is a Python program for a library management system that uses MySQL as a database. The program allows the librarian to add books to the library, lend books to borrowers, and keep track of borrowers&#8217; details, including the book they borrowed, the date it was borrowed, and the borrower&#8217;s contact details.<\/span><\/p>\n<p><span style=\"color: #000000;\">Firstly, the code imports the necessary modules and establishes a connection to the MySQL server. It then checks whether the connection is established successfully or not.<\/span><\/p>\n<p><span style=\"color: #000000;\">The program then creates a database called &#8220;pathsala&#8221; and sets it as the default database. Two tables are created, namely &#8220;books&#8221; and &#8220;borrower,&#8221; to store the data related to books and borrowers, respectively.<\/span><\/p>\n<p><span style=\"color: #000000;\">The program defines several functions to perform various tasks, such as executing SQL commands, fetching data from the database, and calculating the total fare for a book.<\/span><\/p>\n<p><span style=\"color: #000000;\">The function &#8220;all_data(tname)&#8221; is used to display all the data stored in a particular table in a formatted manner. The function first retrieves the column names of the specified table and stores them in a list. It then retrieves all the data from the specified table and prints it along with the column names in a formatted manner.<\/span><\/p>\n<p><span style=\"color: #000000;\">The function &#8220;detail_burrower(name, contact)&#8221; is used to display the details of a particular borrower. It takes the borrower&#8217;s name and contact number as input and displays the details of the borrower in a formatted manner.<\/span><\/p>\n<p><span style=\"color: #000000;\">The function &#8220;days_between(d1, d2)&#8221; is used to calculate the number of days between two dates. It takes two dates as input and calculates the number of days between them.<\/span><\/p>\n<p><span style=\"color: #000000;\">The function &#8220;price_book(days, book_name)&#8221; is used to calculate the total fare for a book borrowed by a borrower. It takes the number of days the book is borrowed and the name of the book as input and calculates the total fare by multiplying the price per day with the number of days.<\/span><\/p>\n<p><span style=\"color: #000000;\">The function &#8220;lend()&#8221; is used to lend a book to a borrower. It displays the list of available books, takes the borrower&#8217;s name, contact number, and the date of borrowing as input and updates the &#8220;borrower&#8221; and &#8220;books&#8221; tables accordingly. It also asks the user whether to lend more books or not.<\/span><\/p>\n<p><span style=\"color: #000000;\">The function &#8220;borrowers()&#8221; is used to display the options available to the librarian. It allows the user to view the details of all borrowers, check the details of a particular borrower, calculate the total fine of a borrower, go back to the previous menu, and exit the program.<\/span><\/p>\n<p><span style=\"color: #000000;\">The function &#8220;tfine()&#8221; is used to calculate the total fine for a borrower. It takes the borrower&#8217;s name and contact number as input, retrieves the book the borrower borrowed, and the date on which the book was borrowed. It then asks the user to enter the date on which the book was returned and calculates the total fine based on the number of days the book was borrowed and the price per day.<\/span><\/p>\n<p><span style=\"color: #000000;\">In summary, the program provides a simple library management system using Python and MySQL. It allows the librarian to manage books and borrowers and perform various tasks such as lending books, calculating fines, and displaying borrower details.<\/span><\/p>\n<p>&nbsp;<\/p>\n<h2><a href=\"https:\/\/cbsepython.in\/class-12-computer-science-project-in-python\/\"><span style=\"text-decoration: underline;\"><span style=\"color: #0000ff; text-decoration: underline;\">Check here for More Projects in Python for Class 11- 12<\/span><\/span><\/a><\/h2>\n","protected":false},"excerpt":{"rendered":"<p>Library Management System Python Project for Class 12 # Python Project Library Management System us MySql Connectivity.\u00a0 &nbsp; import mysql.connector as sqlctr import sys from datetime import datetime mycon = sqlctr.connect(host=&#8217;localhost&#8217;, user=&#8217;root&#8217;, password=&#8217;admin&#8217;) if mycon.is_connected(): print(&#8216;\\n&#8217;) print(&#8216;Successfully connected to localhost&#8217;) else: print(&#8216;Error while connecting to localhost&#8217;) cursor = mycon.cursor() #creating database cursor.execute(&#8220;create database if not [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[43],"tags":[],"class_list":["post-1912","post","type-post","status-publish","format-standard","hentry","category-python-projects"],"_links":{"self":[{"href":"https:\/\/cbsepython.in\/wp-json\/wp\/v2\/posts\/1912","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/cbsepython.in\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cbsepython.in\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cbsepython.in\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/cbsepython.in\/wp-json\/wp\/v2\/comments?post=1912"}],"version-history":[{"count":0,"href":"https:\/\/cbsepython.in\/wp-json\/wp\/v2\/posts\/1912\/revisions"}],"wp:attachment":[{"href":"https:\/\/cbsepython.in\/wp-json\/wp\/v2\/media?parent=1912"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cbsepython.in\/wp-json\/wp\/v2\/categories?post=1912"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cbsepython.in\/wp-json\/wp\/v2\/tags?post=1912"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}