Jeet Project
Jeet Project
Jeet Project
Page | 1
Functions and modules
Modules:
import mysql.connector :-
By importing this package, we are able to establish the
connection between MySQL and Python.
It is not default module in Python we have to install it by
writing following code in Microsoft powershell:
pip install mysql-connector-python
import datetime:-
Python datetime module supplies classes to work with
date and time. These classes provide a number of
functions to deal with dates, times, and time intervals.
Page | 2
Functions:
1) connect( ):
This function establishes connection between Python
and MySQL.
2) cursor( ):
It is a special control structure that facilitates the row-
by-row processing of records in the result set.
The Syntax is:
<cursor object>=<connection object>.cursor()
3) execute( ):
This function is used to execute the sql query and
retrieve records using python.
The syntax is:
<cursor object>.execute(<sql query string>)
4) def( ):
A function is a block of code which only runs when it is
called. def() help user to create their own function.
Page | 3
5) fetchall( ):
This function will return all the rows from the results set
in the form of a tuple containing the records.
6) fetchone( ):
This Function will return one row from the result set in
the form of a tuple containing the records.
7) commit( ):
This function provides changes in the database physically
and permanently.
Page | 4
Detailed DESCRIPTION
Our Project has 3 MySQL tables. These are:-
1)books
2)issue_table
3)return_table
Page | 6
ADDING BOOKS IN TABLE books IN MY SQL
Page | 7
For Python:
import mysql.connector
from datetime import date
def connect():
return mysql.connector.connect(
host="localhost",
user="root",
password="jeet2195",
database="school_library"
)
def create_tables():
db = connect()
cursor = db.cursor()
cursor.execute("CREATE TABLE IF NOT EXISTS books (BookID INT
AUTO_INCREMENT PRIMARY KEY, BookName VARCHAR(255), Author
VARCHAR(255), QTY INT, Subject VARCHAR(255))")
cursor.execute("CREATE TABLE IF NOT EXISTS issue_table (BookID
INT, Student_UID INT, StudentName VARCHAR(255), Issuedate
DATE)")
cursor.execute("CREATE TABLE IF NOT EXISTS return_table (BookID
INT, Student_UID INT, StudentName VARCHAR(255), Returndate
DATE)")
db.close()
Page | 8
sql = "INSERT INTO books (BookName, Author, QTY, Subject)
VALUES (%s, %s, %s, %s)"
val = (bookname, author, total, subject)
cursor.execute(sql, val)
db.commit()
db.close()
Page | 9
sql = "INSERT INTO return_table (BookID, Student_UID,
StudentName, Returndate) VALUES (%s,%s,%s,%s)"
val = (bookid ,studentUID ,studentname ,date.today())
cursor.execute(sql,val)
db.commit()
db.close()
def delete_book(bookid):
db = connect()
cursor = db.cursor()
sql = "DELETE FROM books WHERE BookID = %s"
val = (bookid,)
cursor.execute(sql,val)
db.commit()
db.close()
def display_books():
db = connect()
cursor = db.cursor()
cursor.execute("SELECT * FROM books")
c=cursor.fetchall()
print("\nAVAILABLE BOOKS")
for x in c:
print("\nBookID:",x[0])
print("BookName:",x[1])
print("Author:",x[2])
print("Total book:",x[3])
print("Subject:",x[4])
print('\n')
def report_menu():
Page | 10
while True:
print("\n1. Display Books Issue")
print("2. Display Books Return")
print("3. Back to Main Menu")
choice = int(input("Enter your choice: "))
if choice == 1:
display_issue_table()
elif choice == 2:
display_return_table()
elif choice == 3:
break
else:
print("Invalid choice. Please try again.")
def display_issue_table():
db = connect()
cursor = db.cursor()
cursor.execute("SELECT * FROM issue_table")
print()
c=cursor.fetchall()
for x in c:
print("\nBookID:",x[0])
print("Student_UID:",x[1])
print("StudentName:",x[2])
print("Issuedate:",x[3])
print('\n')
def display_return_table():
db = connect()
cursor = db.cursor()
cursor.execute("SELECT * FROM return_table")
Page | 11
print()
c=cursor.fetchall()
for x in c:
print("\nBookID:",x[0])
print("Student_UID:",x[1])
print("StudentName:",x[2])
print("Returndate:",x[3])
print('\n')
create_tables()
while True:
print("""\n---------------WELCOME TO SCHOOL LIBRARY---------------
\n MENU OF TASKS:
1. Add Book
2. Issue Book
3. Return Book
4. Delete Book
5. Display Books
6. Report Menu
7. Exit """)
if choice == 1:
bookname= input('\nEnter book name: ')
author= input('Enter author name: ')
total= int(input('Enter total number of books: '))
subject= input('Enter subject: ')
add_book(bookname ,author ,total ,subject)
print("Book added Sucessfully!")
Page | 12
elif choice == 2:
bookid= int(input('\nEnter book id: '))
studentUID= int(input('Enter Student UID: '))
studentname= input('Enter Student Name: ')
issue_book(bookid ,studentUID ,studentname)
print("Book Issued Sucessfully to: ",studentname)
elif choice == 3:
bookid= int(input('\nEnter book id: '))
studentUID= int(input('Enter Student UID: '))
studentname= input('Enter Student Name: ')
return_book(bookid ,studentUID ,studentname)
print("Book Returned by : ",studentname)
elif choice == 4:
bookid= int(input('\nEnter book id to delete: '))
delete_book(bookid)
print("Book Deleted Sucessfully!")
elif choice == 5:
display_books()
elif choice == 6:
report_menu()
else:
print("Invalid choice. Please try again.")
Page | 13
output and tables
→OUTPUTS:
1.) Add a Book:
Page | 14
Page | 15
2.) Issue a Book:
Page | 16
3.) Return a Book:
Page | 17
4.) Delete a Book:
Page | 18
5.)Display Books:
Page | 19
Page | 20
Page | 21
Page | 22
6.)Report Menu:
Page | 23
Page | 24
Page | 25
7.)Exit:
Page | 26
→Tables:
1.) SELECT * FROM books;
Page | 27
2.) SELECT * FROM issue_table;
Page | 28
Bibliography
1) https://www.tutorialspoint.com
2) https://github.com
Page | 29