0% found this document useful (0 votes)
3 views

Databse Programs

The document outlines the implementation of a Library management system, an Employee management system, and a Shopping cart system using Python and MySQL. Each section includes database creation, table setup, and functions for various operations such as adding, viewing, and searching records. The code provides a menu-driven interface for user interaction with each system.

Uploaded by

Prafulla BCA
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Databse Programs

The document outlines the implementation of a Library management system, an Employee management system, and a Shopping cart system using Python and MySQL. Each section includes database creation, table setup, and functions for various operations such as adding, viewing, and searching records. The code provides a menu-driven interface for user interaction with each system.

Uploaded by

Prafulla BCA
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

18.

Create a class called Library with Accno, , Title of the book,


author, price and no of copies of the book. Perform the following
operations

1. To accept the details of books and store it in database.


2. To display the details of all books.
3. To find the book with highest cost and display the details of that
book.
4. To display the total cost of all books put together.
5. To search for a particular book

import mysql.connector
mydb=mysql.connector.connect(host="localhost", user="root",
password="")
mycursor=mydb.cursor()
mycursor.execute("create database library")
mydb.close()

import mysql.connector
mydb=mysql.connector.connect(host="localhost", user="root",
password="", database="library")
mycursor=mydb.cursor()
mycursor.execute("create table book(acsn_no varchar(10),book_title
varchar(25),book_author varchar(20),publication
varchar(20),book_price double(10,2),copies integer(10))")
mydb.close()

import mysql.connector
def db_connect():
try:
mydb=mysql.connector.connect(host="localhost", user="root",
password="", database="library")
except:
print("error in db connection")
return mydb
def insert_data(ano,title,author,pub,p,c):
mydb=db_connect()
mycursor=mydb.cursor()
mycursor.execute("insert into book
(acsn_no,book_title,book_author,publication,book_price,copies)
values(%s,%s,%s,%s,%s,%s)",(ano,title,author,pub,p,c))
mydb.commit()
mydb.close()
def view_books():
mydb=db_connect()
mycursor=mydb.cursor()
Page No: 1
mycursor.execute("select * from book")
rows=mycursor.fetchall()
print("accession number \tbook title \tbook author \tpublication \tprice
\tcopies")
for row in rows:
for data in row:
print(data,end="\t")
print()
mydb.close()
def heighest_cost():
mydb=db_connect()
mycursor=mydb.cursor()
mycursor.execute("select * from book where book_price=(select
max(book_price) from book)")
data=mycursor.fetchone()
print("costliest book of the library")
print("Accession number:",data[0])
print("book title:",data[1])
print("Author name:",data[2])
print("publication:",data[3])
print("cost of the book:",data[4])
print("number of copies:",data[5])
def cal_cost():
mydb=db_connect()
mycursor=mydb.cursor()
mycursor.execute("select sum(book_price*copies)from book")
data=mycursor.fetchone()
print("total cost of all the books in the library is:",data[0])
def search_book():
mydb=db_connect()
mycursor=mydb.cursor()
num=int(input("enter the accession no"))
result=mycursor.execute("select * from book where acsn_no=%s",
(num,))
data=mycursor.fetchone()
print("book details")
print("accession no:",data[0])
print("book title:",data[1])
print("Author name:",data[2])
print("publication:",data[3])
print("cost of the book:",data[4])
print("number of copies:",data[5])
while 1:
print("\n1.add book \n2.search for a book \n3.view books \n4.view
costliest book \n5.view total cost \n6.exit")
ch=int(input("enter your choice"))
if ch==1:
Page No: 2
ano=int(input("enter the book accession number"))
title=input("enter the book title")
author=input("enter the book author")
pub=input("enter book publications")
price=input("enter book price")
copies=input("enter the book copies")
insert_data(ano,title,author,pub,price,copies)
if ch==2:
search_book()
if ch==3:
view_books()
if ch==4:
heighest_cost()
if ch==5:
cal_cost()
if ch==6:
print("program terminated")
break
19. Write a menu drive program perform the following operations
on Employee table
i. Insert employee record
ii. Update Salary of all employees.
iii. Display the records

import mysql.connector
mydb=mysql.connector.connect(host="localhost", user="root",
password="")
mycursor=mydb.cursor()
mycursor.execute("create database company1")
mydb.close()

import mysql.connector
mydb=mysql.connector.connect(host="localhost", user="root",
password="", database="company1")
mycursor=mydb.cursor()
mycursor.execute("create table employee(empid varchar(10),empname
varchar(25),empbasic integer(12),empda integer(10),emphra
integer(10),emppf integer(10),empgross integer(12),empnet
integer(12))")
mydb.close()

import mysql.connector
def db_connect():
mydb=mysql.connector.connect(host="localhost", user="root",
password="", database="company1")
Page No: 3
return mydb
def insert_data(id,name,basic):
mydb=db_connect()
mycursor=mydb.cursor()
mycursor.execute("insert into employee (empid,empname,empbasic)
values(%s,%s,%s)",(id,name,basic))
mydb.commit()
mydb.close()
def clac_salary():
mydb=db_connect()
mycursor=mydb.cursor()
mycursor.execute("update employee set
empda=empbasic*0.50,emphra=empbasic*0.10,emppf=empbasic*0.12")
mycursor.execute("update employee set
empgross=empbasic+empda+emphra,empnet=empbasic+empda+emphr
a-emppf")
mydb.commit()
mydb.close()
def display():
mydb=db_connect()
mycursor=mydb.cursor()
result=mycursor.execute("select * from employee")
rows=mycursor.fetchall()
print("empid \t name \t salary \t DA \t HRA \t PF \t GROSS PRICE \t
NET PRICE \n")
for row in rows:
for data in row:
print(data,end=" \t")
print()
mydb.close()
print()
while 1:
print("\n1.Add employee details \n2.Update salary \n3.View employee
details \n4.Exit")
ch=int(input("enter your choice"))
if ch==1:
res='Y'
while res=='Y':
eid=input("enter the employee id")
ename=input("enter the employee name")
esal=int(input("enter the employee salary"))
esal=int(esal)
insert_data(eid,ename,esal)
res=(input("Do you want to continue? Y/N")).upper()
if ch==2:
clac_salary()
print("salary is updated")
Page No: 4
if ch==3:
display()
if ch==4:
print("program terminated!!")
break
20. Write a program to design a shopping cart with the following
options
1. Add item to the cart
2. view cart
3. delete item from the cart

import mysql.connector
mydb=mysql.connector.connect(host="localhost", user="root",
password="")
mycursor=mydb.cursor()
mycursor.execute("create database shopping")
mydb.close()

import mysql.connector
mydb=mysql.connector.connect(host="localhost", user="root",
password="", database="shopping")
mycursor=mydb.cursor()
mycursor.execute("create table product(product_id varchar(10)not null
unique,product_name varchar(25),product_price double(12,2),quantity
integer(5))")
mycursor.execute("create table cart(product_id varchar(10),quantity
integer(5))")
mydb.close()

import mysql.connector
class OutOfStockException(Exception):
pass
def db_connect():
try:
mydb=mysql.connector.connect(host="localhost", user="root",
password="", database="shopping")
except:
print("error in db connection")
return mydb
def view_product():
print("hi")
mydb=db_connect()
mycursor=mydb.cursor()
mycursor.execute("select * from product")
rows=mycursor.fetchall()
Page No: 5
print("product id \t product name \t product price/kg \t quantity \n")
for row in rows:
for data in row:
print(data,end="\t")
print()
mydb.close()
def add_cart():
view_product()
mydb=db_connect()
mycursor=mydb.cursor()
try:
print("\n\n")
pid=input("enter the product id")
qty=int(input("enter the quantity"))
result=mycursor.execute("select * from product where product_id=
%s",(pid,))
item=mycursor.fetchone()
pid=item[0]
pname=item[1]
price=item[2]
pqty=item[3]
if pqty<qty:
raise OutOfStockException
except OutOfStockException:
print("product out of stock")
return
pqty=pqty-qty
print(pqty)
mycursor.execute("insert into cart values(%s,%s)",(pid,qty))
mycursor.execute("update product set quantity=%s where product_id=
%s",(pqty,pid))
mydb.commit()
mydb.close()
def view_cart():
mydb=db_connect()
mycursor=mydb.cursor()
mycursor.execute("select * from cart")
rows=mycursor.fetchall()
print("product id \t\t quantity \n")
for row in rows:
for data in row:
print(data,end="\t\t")
print()
mydb.close()
def remove_cart():
mydb=db_connect()
Page No: 6
mycursor=mydb.cursor()
try:
print("\nyour cart contains the following items\n")
view_cart()
pid=input("enter the product id of the product to be deleted from
cart")
mycursor.execute("delete from cart where product_id=%s",(pid,))
mydb.commit()
print("product",pid,"has been deleted from the cart")
except mysql.connector.Error as err:
print(error)
mydb.close()
while 1:
print("\n\nMenu \n\n1.view product details \n2.Add item to cart \
n3.Remove items to the cart \n4.view cart \n5.exit")
ch=int(input("\nenter your choice"))
if ch==1:
view_product()
if ch==2:
add_cart()
if ch==3:
remove_cart()
if ch==4:
view_cart()
if ch==5:
print("program terminated!!!")
break

Page No: 7

You might also like