Sql-Interface Worksheet
Sql-Interface Worksheet
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)
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
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()
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