Practical File 2024
Practical File 2024
DATABASE PROGRAMMING
Given a table EMP with fields (ENO NAME AND SALARY) which is stored inside the database named test,
Write following programs.
import pymysql
try:
cn=pymysql.connect(host="localhost",user="root",password="1234",db="test")
cursor=cn.cursor()
count=cursor.execute("select * from emp")
rs=cursor.fetchall()
for record in rs:
print(record)
except:
print("unable to connect with database")
cn.close()
Q2. Display all the records from the table field by field
import pymysql
cn=pymysql.connect(host="localhost",user="root",password="1234",db="test")
try:
cursor=cn.cursor()
cursor.execute("select * from emp")
rs=cursor.fetchall()
print("Empno Name Salary")
for record in rs:
eno=record[0]
name=record[1]
salary=record[2]
print(eno," ",name," ",salary)
except:
print("Error in data handling....Process stopped")
cn.close()
import pymysql
cn=pymysql.connect(host="localhost",user="root",password="1234",db="test")
try:
x=int(input("Enter Emp no to search"))
cursor=cn.cursor()
n=cursor.execute("select * from emp where eno=%s"%(x))
rs=cursor.fetchall()
if(n>0):
for record in rs:
print(record)
else:
print("Record is not present")
except:
print("Error in data handling....Process stopped")
cn.close()
import pymysql
cn=pymysql.connect(host="localhost",user="root",password="1234",db="test")
try:
x=input("Enter Emp no to search")
cursor=cn.cursor()
n=cursor.execute("select * from emp where name='%s'"%(x))
rs=cursor.fetchall()
if(n>0):
for record in rs:
print(record)
else:
print("Record is not present")
except:
print("Error in data handling....Process stopped")
cn.close()
import pymysql
cn=pymysql.connect(host="localhost",user="root",password="1234",db="test")
try:
x=int(input("Enter empno to be deleted"))
cursor=cn.cursor()
n=cursor.execute("delete from emp where eno=%s"%(x))
if(n>0):
print("Record deleted successfully")
cn.commit()
else:
print("Record not Found")
except:
print("error")
cn.rollback()
cn.close()
Q5.UPDATING AN EMPLOYEE RECORD ON THE BASIS OF EMPLOYEE NUMBER AND SALARY
INCREMENT
import pymysql
cn=pymysql.connect(host="localhost",user="root",password="1234",db="test")
try:
x=int(input("Enter empno whose salary is to be updated"))
y=int(input("Enter salary increment value"))
cursor=cn.cursor()
N=cursor.execute("update emp set salary=salary+ %s where eno=%s" %(y,x))
if(N>0):
print("Record updated successfully")
cn.commit()
else:
print("Record not found")
except:
print("error")
cn.rollback()
cn.close()
Q6. INSERT AN EMPLOYEE RECORD WITH PROPER VALUES ACCEPTED FROM USER
import pymysql
cn=pymysql.connect(host="localhost",user="root",password="1234",db="test")
try:
x=int(input("Enter employee number"))
y=input("Enter name")
z=int(input("Enter salary"))
cursor=cn.cursor()
n=cursor.execute("insert into emp values(%s,'%s',%s)" %(x,y,z))
if(n>0):
print("Record inserteded successfully")
cn.commit()
else:
cn.rollback()
except:
print("error")
cn.rollback()
cn.close()
TEXT FILES
Q7. read all the lines and display it on the screen using sample.txt file
f=open("sample.txt","r")
line=f.readline()
while line:
print(line)
line=f.readline()
f.close()
f=open("sample.txt","r")
line=f.readline()
c=0
while line:
c=c+1
line=f.readline()
print("Total number of lines = ", c)
f.close()
f=open("sample.txt","r")
line=f.readline()
c=0
x="AaEeIiOoUu"
while line:
if line[0] in x:
c=c+1
line=f.readline()
print("Total number of lines = ", c)
f.close()
f=open("sample.txt","r")
line=f.readline()
c=0
while line:
x=line.split() #x is a list containg words of the present line
c=c+len(x)
line=f.readline()
print("Total number of words = ",c)
f.close()
BINARY FILE
Q11. Consider the binary file emp.dat containing records of employees where each employee record
contains fields employee number, name and salary
write a program that keeps on appending employee records inside this file according to user choice.
Ans
import pickle as p
f=open("emp.dat","ab")
while True:
emprec=[]
empno=int(input("Enter employee number"))
emprec.append(empno)
name=input("Enter employee name")
emprec.append(name)
salary=int(input("Enter salary"))
emprec.append(salary)
p.dump(emprec,f) #it tranfers information of the list emprec to the file emp.dat
choice=input("Do you want to add more employee records y/n")
if choice=='n' or choice=='N':
break
f.close()
Q12. Write a python program that reads contents of binary file emp.dat(Created in previous class) and
display all records on the screen.
Ans.
import pickle as p
f=open("emp.dat","rb")
while True:
try:
emprec=[]
emprec=p.load(f)
eno=int(emprec[0])
name=emprec[1]
salary=int(emprec[2])
print("Employee Number= ",eno,"Name= ",name," Salary= ",salary)
except EOFError:
break
f.close()
Q13. Display those employee records where salary is between 14000 and 28000.
Ans:
import pickle as p
f=open("emp.dat","rb")
while True:
try:
emprec=[]
emprec=p.load(f)
eno=int(emprec[0])
name=emprec[1]
salary=int(emprec[2])
if salary>=14000 and salary<=28000:
print("Employee Number= ",eno,"Name= ",name," Salary= ",salary)
except EOFError:
break
f.close()
Ans.
import pickle as p
f=open("emp.dat","rb")
s=0
c=0
while True:
try:
emprec=[]
emprec=p.load(f)
s=s+int(emprec[2])
c=c+1
except EOFError:
break
average=s/c
print("Average salary = ",average)
f.close()
Q15. Write a python program that accepts employee number and then display his/her details. If
employee is not there, then A messge "Employee not found" should be displayed on the screen.
Ans.
import pickle as p
f=open("emp.dat","rb")
x=int(input("Enter employee number to be searched"))
flag=0
while True:
try:
emprec=[]
emprec=p.load(f)
eno=int(emprec[0])
name=emprec[1]
salary=int(emprec[2])
if eno==x:
print("Employee Number= ",eno,"Name= ",name," Salary= ",salary)
flag=1
break
except EOFError:
break
if flag==0:
print("Employee not found")
f.close()
STACKS
Q16 implement a stack of numbers with the help of list. The program should be menu driven.
def push():
x=int(input("Enter a number to be pushed inside stack"))
stack.append(x)
print("Number pushed successfully")
def pop():
if len(stack)==0:
print("Stack Underflow. Unable to pop an element from empty stack")
else:
x=stack.pop()
print(x," is popped successfully from stack")
def display():
i=len(stack)-1 #if stack has length n, then last element has an index n-1
print("Stack contents are as follows")
while(i>=0):
print(stack[i])
i=i-1
while True:
print("1. Push")
print("2.pop")
print("3.Display")
print("4. Quit")
ch=int(input("Enter you choice"))
if ch==1:
push()
elif ch==2:
pop()
elif ch==3:
display()
else:
break
Searching inside a list
Q17. Write a python function linearsearch(list,size,n) which searches for the number n inside the list.
if the number is found, the function returns its position. However if number is not present inside the
list, the function returns -1
Ans.
def linearsearch(list,size,n):
flag=0
i=0
while(i<size):
if(list[i]==n):
flag=1
break
i=i+1
if(flag==0):
return -1
else:
return i+1