0% found this document useful (0 votes)
34 views

Sql-Interface Worksheet

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views

Sql-Interface Worksheet

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

VELAMMAL BODHI CAMPUS TRICHY

Grade: XII Python Interface with MySQL VENKATESH G, PGT-CS


Worksheet
I. Multiple Choice Question Indicator
Identify the name of connector to establish bridge between Python and
MySQL
1 a) mysql.connection b) connector c) mysql.connect Competency
d) mysql.connector
The connect() method creates a connection to the MySql Server and returns Fill in the
2 __________ . blank
Competency
__________ method fetches all rows in a result set and returns a _________ :
a) fetch(),string b) fetchmany,List c) fetchall(),tuple d) fetchone, Integer
3 Competency

Which method is used to terminate the connection from database?


Statement
4 a) Close() b) exit() c) terminates() d) None of above
Based
Which parameter is an optional in connect() method. Statement
5 a) User b) Host c) databases d) passwd Based
State TRUE or FALSE
6 The connection object is used to access the database and all the tables from Competency
database server.
is_connected() is a built-in function which is used to establish the connection
7 between python and mysql database. Competency

With creation of a database connection object from within a Python program,


8 Competency
a unique session with database starts.
The sql query upon execution via established database connection returns
9 Competency
the result in multiple chunks.
Statement
10 The cursor.rowcount gives the count of records in the resultset.
Based
The cursor.rowcount returns how many rows have been so far retrieved
11 Competency
through fetch..() methods from the cursor.
A DELETE or UPDATE or INSERT query requires commit() to reflect the
12 Competency
changes in the database.
______ object is a special control structure that facilitates the row-by-row
13 processing of records in the resultset. Competency
a) Connection object b) Resultset c) Control object d) cursor object
Statement
14 The excute() method accepts the argument of sql statement in _____ format.
Based
______ is the property of cursor object that is return the number of records
15 retrieved from the resultset. Competency
a) cursor.rowcount b) rowcount.cursor c) fetchall() d) None of above
In the following connection string: Identify the elements:
connect( <<1>> = 127.0.0.1, _ <<2>>_ =‟ root‟, _<<3>>_ = „admin‟)
a. <<1>> = User, <<2>> = password, <<3> = host
16 b. <<1>> = host, <<2>> = user, <<3> = password
Competency
c. <<1>> = host, <<2>> = password, <<3> = user
d. <<1>> = IP, <<2>> = user, <<3> = password
Which function of connection is used to check whether connection to
mysql is successfullydone or not?
import mysql.connector as msq
con = msq.connect( #Connection String ) # Assuming all parameter
required as passed
if :
17 Case Based
print(“Connected!”)
else:
print(“ Error! Not Connected”)

a. con.connected() b. con.isconnected() c. con.is_connected()


d. con.is_connect()
Which of the following component act as a container to hold all the data
18 returned from the query and from there we can fetch data one at a time? Competency
a) Resultset b) Cursor c) Container d) Table
Which attribute of cursor is used to get number of records stored in cursor
(Assumingcursor name is mycursor)?
19 a. mycursor.count b. mycursor.row_count c.mycursor.records
Competency
d. mycursor.rowcount
Which of the Symbols are used for passing parameterized query for execution
Statement
20 to cursor?
a) % b) {} c) $ d) Both a and b Based
Which function is used to fetch n number of records from cursor? Statement
21 a) fetch() b) fetchone() c) fetchmany() d) fetchall() Based
Which cursor function is used to send query to connection?
22 a) query() b) execute() c) run() d) send() Competency

Consider the information stored in the table : EMP


EMPNO ENAME DEPT SALARY
1 ALEX MUSIC 60000
2 PETER ART 67000
3 JOHNY WE 55000
4 RAMBO P&HE 48000
Following python code is written to access the records of table: EMP, What
will be the output of following code:

23 # Assume All basic setup related to connection and cursor creation is already Case Based
done query=”select * from emp”
mycursor.execute(query)
results = mycursor.fetchone()
results = mycursor.fetchone()
results = mycursor.fetchone()
d = int(results[3])
print(d*3)

a) P&HEP&HEP&HE b) 144000 c) WEWEWE d) 165000


Consider the following Python code is written to access the record of CODE
passed to function:
Complete the missing statements:

def Search(eno):
#Assume basic setup import, connection and cursor is created
24 query=”select * from emp where empno=_______”.format(eno)
Case Based
mycursor.execute(query)
results = m ycursor.
Print(results)
a) { } and fetchone() b) fetchone() and { }
b) c) %s and fetchone() d) %eno and fetchone()
Consider the following Python code for updating the records:
def Update(eno):
#Assume basic setup import, connection(con) and cursor(mycursor) is
#created
query=”update emp set salary=90000 where empno=”+str(eno)
25 mycursor.execute(query) Case Based

Code is running but the record in actual database is not updating, what
could be the possible reason?
a) save() function is missing b) con.save() function is missing
c) con.commit() function is missing d) commit() function is missing
Assertions and Reasons Competency
Assertion. A database connection object controls the connection to a
26 database.
Reason. A connection object represents a unique session with a database,
connected from within a script/program.
Assertion. A database cursor receives all the records retrieved as per the Competency
query.
27
Reason. A resultset refers to the records in the database cursor and allows
processing of individual records in it.
Assertion. The database cursor and resultset have the same data yet they Competency
are different.
28
Reason. The database cursor is a control structure and the resultset is a
logical set of records.
Assertion. One by one the records can be fetched from the database Competency
directly through the database connection.
29
Reason. The database query results into a set of records known as the
resultset.
Assertion. MySqldb is an interface for connecting to a MySql database Competency
30 servers from python.
Reason. The exit() method is used to close the connection to the database

II.Short Answer Type Questions


What are the steps to connect to a database from within a Python
31 Conceptual
application?
32 What is a resultset? Direct

Write code to connect to a MySQL database namely School and then fetch
33 Conceptual
all those records from table Student where grade is ' A' .
Predict the output of the following code :
import mysql.connector
db = mysql.connector.connect(....)
cursor = db.cursor()
34 sql1 = "update category set name = '%s' WHERE ID = %s" % ('CSS',2) Conceptual
cursor.execute(sql1)
db.commit()
print("Rows affected:", cursor.rowcount)
db.close()

III. Long Answer Type Questions


Schema of table EMPL is shown below :
EMPL (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO) Application
35 Write python code to obtain search criteria from user and then fetch Based
records based on that from empl table. Questions

Given the below is the table item in database inventory.

Application
36 Based
Questions
Riya Createed this table but forget to add column ManufacturingDate. Can
she add this column after creation of table? If yes, Write the code where
username and password are system and test respectively.
Consider the table Faculty whose column’s names are
F_id, F_name, Lname, Hiredate, salary, Course_name
Write the code to insert the following record into the above table.
37 Competency

i. Define the term Domain with respect to RDBMS. Give one example to support
your answer.
ii. Kabir wants to write a program in Python to insert the following
record in the table named Student in MYSQL database, SCHOOL:
 rno(Roll number )- integer
 name(Name) - string
 DOB (Date of birth) – Date
38  Fee – float Competency
Note the following to establish connectivity between Python and
MySQL:
 Username - root
 Password - tiger
 Host - localhost
The values of fields rno, name, DOB and fee has to be accepted from
the user. Help Kabir to write the program in Python.
Sartaj has created a table named Student in MYSQL database, SCHOOL:
rno(Roll number )- integer
39 name(Name) - string Competency
DOB (Date of birth) – Date
Fee – float
Note the following to establish connectivity between Python and MySQL:
Username - root
Password - tiger
Host – localhost
Sartaj, now wants to display the records of students whose fee is more than 5000. Help
Sartaj to write the program in Python
Consider the table Student whose fields are.

Write the Python code to update grade to ‘A’ for all these students who are getting
more than 8 as points.
The table structure is as follows:
40  Scode : integer Case Based
 Name : varchar
 Age : integer
 Strcde : integer
 Points : integer
 Grade : varchar
Note the following to establish the connection between Python and MySQL:
 Host: localhost
 Username : Admin
 Password : Admin@123
The table exists in MySQL database as: Student

The code given below deletes the record from the table employee which contains the
following record structure:
E_code – String
E_name – String
Sal – Integer
City- String
Note the following to establish connectivity between Python and MySGQL:
Username is root
Password is root
The table exists in a MySQL database named emp.
The details (E code,E_name, Sal, City) are the attributes of the table.
Write the following statements to complete the code :
41 Case Based
Statement 1 – to import the desired library.
Statement 2 – to execute the command that deletes the record with E_code as ‘El01’.
Statement 3 – to delete the record permanently from the database.
import __________ as mysql # statement 1
def delete():
mydb=mysql.connect(host=”localhost”,user=”root”,passwd=”root”,database=”emp”)
mycursor=mydb.cursor()
______________ # Statement 2
______________ # Statement 3
Print(“Record deleted”)
Tne code given below reods the following records from the table employee and
displavs only those records who have employees coming from city ‘Delhi’:}
E_code – String
E_name – String
Sal – Integer
City- String
Note the following to establish connectivity between Python and MySGQL:
Username is root
Password is root
The table exists in a MySQL database named emp.
The details (E code,E_name, Sal, City) are the attributes of the table.
Write the following statements to complete the code :
Statement 1 – to import the desired library.
Statement 2 – to execute the query that fetches records of the employees coming from
city ‘Delhi’.
Statement 3 – to read the complete data of the query (rows whose city is Delhi) into the
42 object named details, from the table employee in the database. Case Based

You might also like