0% found this document useful (0 votes)
8 views15 pages

Comp

Uploaded by

sahityamishra29
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)
8 views15 pages

Comp

Uploaded by

sahityamishra29
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/ 15

1.

Write a program in python to extract all the data of table


student from the database factory and print it.

import mysql.connector as sqltor

mycon=sqltor.connect(host='localhost',user='root',password='akash@123',database='factory')

if mycon.is_connected:

print('connected to mysql')

cursor=mycon.cursor()

cursor.execute('select * from student')

data=cursor.fetchall()

r=cursor.rowcount

print(r)

for i in data:

print(i)
2. Write a program to make a dictionary of product Sold to
the customer with its price and print it.

x={}

for i in range(8):

p=input("Enter Product name")

r=int(input("Enter its Price"))

x[p]=r

print(x)

n=input("Enter Product to be Seached")

f=0

for i in x:

if i==n:

print("found",x[i])

f=1

break

if f==0:

print("no such product found")


3. Write a menu drive in program in python For my SQL
database factory with following functions insert_r(),
Display_r(),delete_r() update_r() ,exit_from_database() of
table student with field name r-int primary key,n-
varchar(30),m-int,g-char(),s-char(),
p-varchar(20).

def insert_r():

cursor=my.cursor()

r=int(input('enter roll no.'))

n=input('enter name')

m=int(input('enter marks'))

g=input('enter grade')

s=input('enter section')

p=input('enter project record')

st="insert into student Values({},'{}',{},'{}','{}','{}')".format(r,n,m,s,g,p)

cursor.execute(st)

my.commit()

print('record inserted sucessfully')

return

def display_r():

cursor=my.cursor()

st='select *from student'

cursor.execute(st)

data=cursor.fetchall()

for i in data:
print(i)

return

def delete_r():

cursor=my.cursor()

s=input("enter grade for record deletion")

st="delete from student where grade='{}'".format(s)

cursor.execute(st)

my.commit()

print('record deleted sucessfully')

return

def update_r():

cursor=my.cursor()

st="update student set grade='{}' where section='{}'".format('B','B')

cursor.execute(st)

my.commit()

print('record updated sucessfully')

return

def exit_from_d():

my.close()

return

import mysql.connector as s

my=s.connect(host='localhost',user='root',password='akash@123',database='factory')

if my.is_connected():

print('sucessfully connected to mysql')

cursor=my.cursor()

ans='y'

while ans=="y":

print('main menu')

print('1.insert record')
print('2.display record')

print('3.delete record')

print('4.update record')

print('5.exit from database')

c=input("enter user choise")

if c=='1':

insert_r()

elif c=='2':

display_r()

elif c=='3':

delete_r()

elif c=='4':

update_r()

elif c=='5':

exit_from_d()

break

print("want to excute main menu")

ans=input("enter 'y' to continue or 'n' to stop")

if ans!='y':

my.close()

ans='n'
4. Write a program in python to write some text in text file
akash .txt

f=open("C:\\Users\\Akash\\OneDrive\\Documents\\akash .txt",'a+')

c=f.read()

print(c)

a='1'

while a=='1':

s=input("enter a string")

f.write(s)

a=input("enter 1 to add a next string")


5. Write a menu drive in program in python to read or write a
binary file newI.dat .

import pickle

def write():

f=open("C:\\Users\\Akash\\OneDrive\\Desktop\\Newl.dat",'wb')

l=eval(input("enter a list"))

d=eval(input("enter a dictionary"))

pickle.dump(l,f)

pickle.dump(d,f)

f.close()

def read():

f=open("C:\\Users\\Akash\\OneDrive\\Desktop\\Newl.dat",'rb')

try:

print("record=")

while True:

a=pickle.load(f)

print(a)

except EOFError:

print("program Excuted")

f.close()

a=input("enter y to input \n enter n to read \n press any key to exit")

while a in ['y','n']:

if a=='y':

write()

else:

read()

a=input("enter y to input \n enter n to read \n press any key to exit")


6. Write a program in python to create a dictionary in binary
file new I.dat that stores Employees information such as
employee no, name, salary and their designation

import pickle

f=open("C:\\Users\\Akash\\OneDrive\\Desktop\\Newl.dat",'rb+')

d={}

for i in range(5):

d['empno']=int(input("enter enployee no"))

d['empname']=input("enter enployee name")

d['empsal']=int(input("enter enployee salary"))

d['empdesig']=input("enter enployee designation")

pickle.dump(d,f)

try:

print("record of file")

while True:

s=pickle.load(f)

print(s)

except EOFError:

print("program excuted")

f.close()
7. Write a program in python to write student details in the
CSV file new.csv .

import csv

def write():

f=open("C:\\Users\\Akash\\OneDrive\\Documents\\New.csv",'a+',newline='\r\n')

w=csv.writer(f)

w.writerow(['Rollno.','Name','subject','Marks'])

n=int(input("Enter no of rows to be inserted"))

for i in range(n):

r=int(input("enter SR No."))

n=input("enter name")

s=input("enter subject")

m=int(input("enter marks"))

w.writerow([r,n,s,m])

print("data added")

f.close()

def read():

with open("C:\\Users\\Akash\\OneDrive\\Documents\\New.csv",'r',newline='\r\n') as f:

r=csv.reader(f)

for i in r:

print(i)

print("over")

f.close()

a='1'

while a=='1':

c=input("enter w to write \t \tenter r to read")

if c=='w':
write()

elif c=='r':

read()

else:

break

a=input("enter 1 to continue")

if a not in['1']:

print("wrong choise")

You might also like