SQL Connectivity - Revision
SQL Connectivity - Revision
1. The following table represents information on sales representatives of ABC company with the
following data.
Sales man name, Code , Address, commission , salary.
Write Python code to create the above table in database ‘db’.
2.Write Python mysql Pgm to retrieve all the data from table student which is in database ‘db’.
3. 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
A python code is written to access the records of table: EMP, What will be the output of following
code:
# Assume All basic setup related to connection and cursor creation is already done
query="select * from emp"
mycursor.execute(query)
results = mycursor.fetchone()
results = mycursor.fetchone()
results = mycursor.fetchone()
d = int (results[3])
print (d*3)
4.A ------------------ is a special control structure that facilitates the row by row processing of records
in the resultset.
5.After importing mysqlconnector, first of all --------------is established by using connect()
6.-----------------method executes a database query from within Python.
7. Running of sql query through database cursor returns the table records in the form of---------
8.A connectivity package---------------must be imported before running db connection program.
9.Which of the following is not a legal method for fetching records from database.
a)fetchone() b)fetchtwo() c)fetchall() d)fetchmany()
10.To fetch one record from resultset you may use<curor>………… method.
a)fetch() b)fetchone() c)fetchtuple d)none of these.
Answers:
1.
import mysql.connector
mydb=mysql.connector.connect(host="localhost",user="root",passwd="system",database=
"sales") mycursor=mydb.cursor()
mycursor.execute("CREATE TABLE SALESMAN (NAME VARCHAR(20),CODE INT ,ADDRESS
VARCHAR(20), COMMISSION DEC,SALARY FLOAT);")
2.
import mysql.connector
conn=mysql.connector.connect(host="localhost",user="root",passwd="system",database=
"DB") c=conn.cursor()
c.execute("select * from student")
r=c.fetchone()
while r is not None:
print(r)
r=c.fetchone()
conn.close()
3. 165000
4.database cursor
5.database connection.
6. execute()
7.resultset
8.mysql.connector
9. b)fetchtwo()
10. b)fetchone()
Revision Test - 2
1.Write a python code to delete all the records from employee table whose age >60 and the table has
the following fields. Empid, empname, deptid, age, payscale
2. Consider the following Python code is written to access the details of employee, whose employee
number is passed to function: Complete the missing statements:
def Search(eno):
import mysql.connector
mydb=mysql.connector.connect(host="localhost",user="root",passwd="system",database="D
B")
mycursor=mydb.cursor()
query="select * from emp where empno=___".format(eno)
mycursor.execute(query)
results = mycursor.
print(results)
2. .{ } and fetchone()
4. con.is_connected()
5. fetchall() function is used to fetch all the records from the cursor in the form of tuple.
fetchone() is used to fetch one record at a time. Subsequent fetchone() will fetch next
records. If no more records to fetch, it returns None.
6
import mysql.connector
mydb=mysql.connector.connect(host="localhost",user="root",passwd="system",database="
DB")
mycursor=mydb.cursor()
s= input(“enter the name”)
mycursor.execute("delete from emp where name =’{}’)”.format(s)
mydb.commit()
Revision Test - 3
1) ___________ helps in efficient retrieval, inserting and deleting of data.
2) ___________ is a software for creating and managing databases.
3) _________ means that data is accurate and consistent in the database.
4) ______________- ensure that the database properly changes states upon a successfully committed
transaction.
5) RDBMS stands for __________.
6) In RDBMS a database is considered as a collection of ___________
7) Collection of rows and columns are called as ______________
8) Record is also called as a ____________
9) The group of one or more columns used to uniquely identify each row of a relation is called________
10) __________ is data about data.
11) A _________ is a request to a database for obtaining information in a desired way.
12) ___________ is collection of values from which the value is derived for a column.
13) _______ are the columns of a table that points to the primary key of another table.
14) There is a column HOBBY in a Table CONTACTS. The following two statements are giving different
outputs. What may be the possible reason?
a.SELECT COUNT(*) FROM CONTACTS;
b.SELECT COUNT(HOBBY)FROM CONTACTS;
15) What will be the output of the following queries on the basis of Employee table:
16
(i) Select Cname, Charges from Car where Colour=’silver’;
(ii) Select distinct Ccode from customer;
(iii) Select min(Charges), max(Charges) from Car;
(iv) Update Car set Charges=Charges - Charges*0.1 from Car R, Customer C where
R.Ccode=C.Ccode;
(v) Select Cname, Make from Car where Charges between 2000 and 3000;