0% found this document useful (0 votes)
12 views2 pages

Cs Prac18

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

Cs Prac18

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

17.

PYTHON MYSQL INTERFACE

AIM: To create a connectivity and provide an interface between Python and


MySQL to perform queries using DML commands
Create a table STUDENT using python interface having atleast 5 records with the
following attributes :
ROLL,NAME,TOTAL,COURSE,DOB. Also apply constraints PRIMARY KEY for ROLL
and CHECK for COURSE to take values only from ‘CS’, ‘BIOLOGY’, ‘COMMERCE’,
‘EG’. Also write queries to perform the following operations:
A. Find maximum total marks, minimum total marks and number of students in each
course where there at least 2 students
B. Display details ofstudents born in the month of may 2003 who have scored total
between 100 to 200.

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

HIGHEST LOWEST COUNT


495.0 187.95 4
409.0 250.0 4
455.0 150.0 4

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

You might also like