0% found this document useful (0 votes)
4 views6 pages

Practical 1

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)
4 views6 pages

Practical 1

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/ 6

# SEARCH IN BINARY FILE

import pickle
def search(file):
flag = False
r = int(input("Enter roll no to be searched :"))
while True:
try:
rec = pickle.load(file)
if rec['Rollno'] == r:
print('Roll Num:', rec['Rollno'])
print('Name:', rec['Name'])
print('Marks:', rec['Marks'])
flag = True
except EOFError:
break
if flag == False:
print('No Records found')
#main
try :
f = open('student.dat','rb')
search(f)
except Exception as e :
print(e)
finally:
f.close()
# READ AND WRITE IN CSV FILE
import csv
def write_csv(nfw):
ch='y'
while ch=='y':
userid = input("enter userid :")
password = input("Enter password :")
nfw.writerow([userid,password])
ch = input("More entry?(y/n)")

def read_csv(newFileReader):
for row in newFileReader:
print("%10s"%row[0],"%15s"%row[1])

# main

while True:
print("1. WRITE IN CSV FILE")
print("2. READ IN CSV FILE")
print("3. EXIT")
ch = input("enter your choice :")
if ch == "1" :
try :
newFile = open('Book1.csv','w',newline = '')
nfw = csv.writer(newFile)
write_csv(nfw)
except Exception as e:
print(e)
finally :
newFile.close()
elif ch == "2" :
try :
newFile = open('Book1.csv','r')
newFileReader = csv.reader(newFile)
read_csv(newFileReader)
except Exception as e:
print(e)
finally :
newFile.close()
elif ch == "3":
break
else :
print("INVALID CHOICE")
# STACK OPERATION
s=[]
def push(a):
s.append(a)
def pop():
if(s==[]):
print("Stack Underflow")
else:
print("Element Deleted is :",s.pop())
def peek():
if s == []:
print("Empty Stack")
else:
top = s[-1]
print("Top most element is :",top)
def display():
l=len(s)
for i in range(l-1,-1,-1):
print(s[i])
def menu():
c="y"
while(c=="y"):
print("1. PUSH")
print("2. POP")
print("3. PEEK")
print("4. DISPLAY")

choice=int(input("Enter your choice :"))


if(choice==1):
a=input("Enter any number: ")
push(a)
elif (choice==2):
pop()
elif(choice==3):
peek()
elif (choice==4):
display()
else:
print("Wrong Input")
c=input("Do you want to continue? (y/n):").lower()
menu()
# DATABASE CONNECTIVITY – UPDATE
import mysql.connector
mydb=mysql.connector.connect(host="localhost",\
user="root",\
passwd="root",\
database="employee")
mycursor=mydb.cursor()

def employeeupdate():
e=int(input("Enter the empno to update:"))
sqll=("select * from emp where empno=%s")
rl=(e,)
value=int(input("Enter the amount to be added:"))
sql=("update emp set sal = sal+%s where empno=%s")
data=(value,e)
mycursor.execute(sql,data)
mydb.commit()

employeeupdate()

You might also like