Cs Prac18
Cs Prac18
PROGRAM CODING:
import mysql.connector as m
con=m.connect(host='localhost',user='root',password='rsk123',database='school')
cur=con.cursor()
while True:
r=int(input('Roll='))
n=input('Name=')
t=int(input('Total='))
c=input('course=')
d=input('dob=')
cur.execute("insert into student values({},'{}',
{},'{}','{}')".format(r,n,t,c,d))
con.commit()
ans=input('continue(y/n)')
if ans in 'yY':
continue
else:
break
print('HIGHEST','LOWEST','\t','COUNT')
cur.execute("select max(total),min(total),count(*) from student group by course
having count(*)>2")
data=cur.fetchall()
for i in data:
print(i[0],'\t',i[1],'\t',i[2])
print("\nStudent's details whose have born on May 2003 and scored total from 100 to
200")
cur.execute("select * from student where dob like '2003-05-%' and 100<=total<=200")
data=cur.fetchall()
for i in data:
print(i)
con.close()
OUTPUT:
Roll=111
Name=JOE
Total=250
course=BIOLOGY
dob=2003-05-23
continue(y/n)N
Student's details whose have born on May 2003 and scored total from 100 to 200
(109, 'Jhansi', 150.0, 'commerce', datetime.date(2003, 5, 15))
(111, 'JOE', 250.0, 'BIOLOGY', datetime.date(2003, 5, 23))
-----------------------------------------------------------------------------------
-----------------
18. PYTHON MYSQL INTERFACE
AIM:
To create a connectivity and provide an interface between Python and
MySQL to perform queries using DML commands
A. Sort the student list in the descending order of total.
B. Increase total marks for CS students by 5 %, for those, whose total is less than
180.
PROGRAM CODING:
import mysql.connector as m
con=m.connect(host='localhost',user='root',password='rsk123',database='school')
cur=con.cursor()
q="select * from student order by roll desc"
cur.execute(q)
data=cur.fetchall()
for i in data:
print(i)
q="update student set total=total+0.05*total where course='cs'and total<180"
cur.execute(q)
con.commit()
print(cur.rowcount,'Record(s) updated')
con.close()
OUTPUT:
(110, 'Jeeva', 495.0, 'cs', datetime.date(2008, 3, 4))
(109, 'Jhansi', 390.0, 'commerce', datetime.date(2007, 9, 24))
(108, 'Mithran', 270.0, 'biology', datetime.date(2008, 12, 25))
(107, 'vishnu', 470.0, 'cs', datetime.date(2007, 12, 19))
(106, 'ARUN', 314.0, 'COMMERCE', datetime.date(2008, 2, 16))
(105, 'TARUN', 409.0, 'BIOLOGY', datetime.date(2007, 12, 25))
(104, 'SARA', 179.0, 'CS', datetime.date(2008, 2, 17))
(103, 'TOM', 356.0, 'COMMERCE', datetime.date(2008, 1, 2))
(102, 'SAM', 250.0, 'BIOLOGY', datetime.date(2007, 6, 22))
(101, 'RAM', 445.0, 'CS', datetime.date(2007, 9, 12))
1 Record(s) updated