SQL Connectivity Class Test Qp&Ms 2
SQL Connectivity Class Test Qp&Ms 2
Consider the table Employee in the database “PAYROLL” with the following
credentials : Host – “localhost”, username – “root”, password – “sql@123”
Table - EMPLOYEE
EMPNO ENAME DESIG SALARY LEAVE BONUS
121 RAM SUNDAR MANAGER 50000 3 3000
122 KAVYA SALES EXECUTIVE 30900 4 2300
123 FARAZ SUPERVISOR 40200 2 2400
Complete the given Python program by filling up the blank lines to connect with
import mysql.connector as ms
mycon=ms.connect(________________________________) #statement 1
if mycon.is_connected():
print("Successful")
cursor=_________________ #statement 2
cursor.execute("create table Employee(empno int primary key, ename varchar(20),
desig varchar(20),salary float(8,2),leave int, bonus float(7,2))")
mycon.commit()
for i in range(3):
eno=int(input("Enter the Employee number "))
ename=input("Enter the Employee name ")
desig=input("Enter the designation ")
sal=float(input("Enter the salary "))
lea=float(input("Enter the leave details "))
bon=float(input("Enter the bonus details "))
cursor.execute(________________________________) # statement 3
cursor.execute("select * from Employee")
data=______________________ #statement 4
count=cursor.rowcount
print("Rows: ",count)
for row in data:
print(row)
i) Complete the statement #1 to write appropriate missing parameters.
(Use default values for the parameters)
ii) Write Statement #2 to create the cursor object
iii) Write the necessary command for statement #3 to insert the record from the inputs
given by the user
iv) Complete the statement #4, to fetch all the records from the result set.
Answers
Table - STUDENT
ADMNO SNAME GENDER DOB STREAM AVERAGE
JC1234 KAMAL MALE 2000-09-09 SCIENCE 94
JC1324 SUPRIYA FEMALE 2001-08-07 COMMERCE 76
JC3456 VARAHAN MALE 2002-07-06 SCIENCE 67
Complete the given Python program by filling up the blank lines to connect with
import mysql.connector as sq
mycon=sq.connect(host="localhost",user="root",passwd="admin",database="school")
if mycon.is_connected():
print("Successful")
cursor=_____________ #statement 1
cursor.execute(_________________________) #statement 2
cursor.execute("insert into Student values (‘JC1234’,’KAMAL’,’MALE’,’2000-09-09’,’SCIENCE’,94)”)
cursor.execute("insert into Student values (‘JC1324’,’SUPRIYA’,’FEMALE’,’2001-08- 7’, ’COMMERCE’,76)”)
cursor.execute("insert into Student values(‘JC3456’,’VARAHAN’,’MALE’,’2002-07-06’,’SCIENCE’,67)”)
mycon.__________________ #statement 3
cursor.execute(_______________) #statement 4
data=cursor.fetchall()
print(data)
1. Cursor=mycon.cursor()
2. Cursor.execute(“create table STUDENT(ADMNO varchar(10), SNAME varchar(10),
GENDER varchar(10), DOB date, SNAME varchar(10), AVERAGE int)”)
3. mycon.commit()
4. Cursor.execute(“select * from STUDENT where average>90
3. Consider the tables WORKER and PAYLEVEL are already created and
available in the database ADMIN with the following credentials:
Host – “localhost”, username – “root”, password – “sql@123”
Table - WORKER
ECODE NAME DESIG PLEVEL DOJ DOB
11 RADHESHYAM SUPERVISOR P001 2004-09-12 1981-08-23
15 SANYA CLERK P002 2005-12-19 1983-06-09
18 SARSA SUPERVISOR P001 2010-01-20 1982-02-01
Table - PAYLEVEL
import mysql.connector
mycon= mysql.connector.connect(_____________________) #statement 1
cursor=mycon.cursor()
cursor.execute(_______________________) # statement 2
data=cursor.fetchall()
for row in data:
print(row)
cursor.execute(_____________________) #statement 3
mycon.__________________ #statement 4
Answers
Table - Event
EventId Eventname NumPerformers CelebrityID
101 Birthday 10 C102
102 Promotion Party 20 C103
103 Engagement 12 C102
104 Wedding 15 C104
105 Birthday 17 C101
Table - Celebrity
CelebrityID Name Phone FeeCharged
C101 Faiz Khan 9910195610 200000
C102 Sanjay Kumar 8934664481 250000
C103 NeeraKhan Kapoor 9811665685 300000
C104 Reena Bhatia 6587775645 100000
Complete the given Python program by filling up the blank lines to connect with
MySQL using database connectivity as directed in the questions (i) to (iv)
import mysql.connector
mycon= mysql.connector.connect(host="localhost",user="root",
passwd="admin",database="Media”)
cursor=_____________ #statement 1
cursor.execute(_______________________) # statement 2
data=___________________ # statement 3
for row in data:
print(row)
cursor.execute(_____________________) # statement 4
mycon.commit()
i) Complete the statement #1 to create the cursor object.
ii) Write Statement #2 to display the Eventname, Name of celebrity and Feecharged
for those celebrities who charge more than 200000
iii) Write Statement #3 to fetch the first 2 records from the result set.
iv) Write the necessary command for statement #4 to delete the records of the
celebrity table whose name starts with “R”.
Answers
i. cursor=mycon.cursor()
ii. cursor.execute(“select Eventname, Name, Feecharged from Event, Celebrity
where Event.CelebrityID = Celebrity. CelebrityID and feecharged > 200000”)
iii. data=cursor.fetchmany(2)+
iv. cursor.execute(“delete from Celebrity where name like ‘R%’ “)
5. Consider the tables Sports and Coach are already created and available in the
database Admin
Table - Sports
Scode Sportsname Participants Prizemoney Scheduledate
101 Carrom 2 50000 2012-01-23
103 Table Tennis 4 8000 2012-02-14
105 Chess 2 9000 2012-01-01
Table - Coach
Code Name Scode
1 Ravi 101
2 Mohan 108
3 Sameer 101
Complete the given Python program by filling up the blank lines to connect with
MySQL using database connectivity as directed in the questions (i) to (iv)
import mysql.connector as ms
mycon= ms.connect(host="localhost",user="root",passwd="1234",database="admin”)
cursor=_____________ #statement 1
cursor.execute(_______________________) # statement 2
data=___________________ # statement 3
for row in data:
print(row)
cursor.execute(_____________________) # statement 4
mycon.commit()
Answers
i. cursor=ms.cursor()
ii. cursor.execute(“select Sports.*,Name from Sports, Coach where
Sports.scode=Coach.scode and Prizemoney >9000”)
iii. data=cursor.fetchone()
iv. cursor.execute(“update Sports set Participants=Participants+6 where Sportsname
in (‘Carrom’, ‘Chess’, ‘badminton’)”)
6. Consider the tables Teacher and Posting are already created and available
in the database ‘School’ with the following credentials:
Host – “localhost”, username – “root”, password – “sql@123”
Table - TEACHER
T_ID NAME AGE DATE_OF_JOIN DEPT SALARY
T102 VANIPRIYA 36 10/01/2017 COMMERCE 12000
T104 JAYALAKSHMI 45 24/03/2008 ENGLISH 20000
T101 MOHAN 54 12/12/2016 PHYSICS 30000
T103 JUGAL 34 01/07/2015 COMPUTER 40000
SCIENCE
Table - POSTING
P_ID DEPT PLACE
1 HISTORY AGRA
2 MATHEMATICS RAIPUR
3 COMPUTER SCIENCE DELHI
Complete the given Python program by filling up the blank lines to connect with
MySQL using database connectivity as directed in the questions (i) to (iv)
import mysql.connector as ms
mycon= ms.connect(_____________________) #statement 1 cursor=mycon.cursor()
cursor.execute(_______________________) # statement 2
data=cursor.fetchall()
for row in data:
print(row)
cursor.execute(_____________________) # statement 3
mycon.__________________ #statement 4
Table - Student
ADMNO SNAME GENDER DOB STREAM FEES
JC1234 KAMAL MALE 2000-09-09 SCIENCE 45000
JC1324 SUPRIYA FEMALE 2001-08-07 COMMERCE 40000
JC3456 VARAHAN MALE 2002-07-06 SCIENCE 45000
Table - Stucontact
Complete the given Python program by filling up the blank lines to connect with
MySQL using database connectivity as directed in the questions (i) to (iv)
import mysql.connector as ms
mycon= ms.connect(host="localhost",user="root",passwd="1234",database="school”)
cursor=_____________ #statement 1
cursor.execute(_______________________) # statement 2
data=___________________ # statement 3
for row in data:
print(row)
cursor.execute(_____________________) # statement 4
mycon.commit()
Answers:
i. cursor=mycon.cursor()
ii. cursor.execute(“Select Sname, Fname, Mname from Student, Stucontact where
Student.admno=Stucontact.admno and gender=’Female’ “)
iii. data=cursor.fetchall()
iv. cursor.execute(“Delete from Student”)