Interfaceworksheet 24
Interfaceworksheet 24
THEORY QUESTIONS
2. A: mydb = mysql.connector.connect
(host="192.68.45.251",user="you",
password="y")
R: host variable should be initialized with value
‘localhost’
3. A:mydb = mysql.connector.connect(
host="178.23.45.262", user="you",password="y")
R: database name has not been provided in the
connect function call.
4. A:con= # Assume connection is already created
data = [('Jane', 'F'),('Joe', 'M'),('John', 'M'),]
cur=con.cursor()
stmt= "INSERT INTO employees (fname, hire_date)
VALUES (%s, %s)"
cur.execute(stmt, data)
mydb.commit()
R: execute function can’t be used to insert multiple
rows.
5. A: An user run the following command to input a new
record in the table cur.execute("insert into students
values(1,'Anu',78.50,'B1'") but he found that record
cannot be inserted in the table .
R: Commit() function must used to save the changes
and reflect the data in the table.
6. A: To retrieve Three(3) students details we use
cursor.fetchmany(3) function.
R: The number of rows is a compulsory parameter for
cursor.fetchmany( 3)
7. A: To create a table in MySQL, use the "CREATE
TABLE"statement.
R: We can use cursor.run() to create the table.
8. A:mydb = mysql.connector.connect( host="localhost",
user="you",password="you")
mycursor = mydb.cursor()
mycursor.execute("CREATE DATABASE mydatabase")
R: We can create a new database using execute
function.
9. A: cur.execute("DELETE FROM customers WHERE
address = "M")
R: Commit function should be called to save the
changes
10. A: The resultset refers to a logical set of records
that are fetched from the database by executing
an SQL query.
R: Result set stored in a cursor object can be
extracted by using fetch(…) functions
OBJECTIVE TYPE QUESTIONS (MCQ)
1. Which of the following is the correct set of commands for installing
and importing mysql connector, respectively?
(a) pip install mysql.connector import mysql.connector
(b) pip install mysql-connector import mysql.connector
(c) pip install mysql-connector import mysql-connector
(d) pip install mysql.connector import mysql-connector
2. Which my sql driver you need to install for connection of Python
With MYSQL
(a) mysql-connector (b) mysql.connector
(c) mysql-connect (d) All of the above
3. Mandatory arguments required to connect any database from
Python are:
(a) Username, Password, Hostname, Database name, Port
(b) Username, Password, Hostname
(c) Username, Password, Hostname, Database Name
(d) Username, Password, Hostname, Port
4. What is the maximum number of parameters that can be accepted
by connect method.
(a) 2 (b) 3 (c) 4 (d) 0
5. The ............. creates a connection to the MySQL server and returns a
Connection object.
(a) connect() (b) connection() (c) connector() (d) None above
6. Python enables Python programs to access MySQL databases
(a) import mysql.connect (b) import mysql.connector
(c) import mysql.connection (d) None of the above
7. The -------------------- constructor creates a connection to the
MySQL server andreturns a MySQL Connection object.
(a) connect() (b) connection()
(c) mysqlconnect() (d) None of the above
8. Maximum how many parameters can be accepted by cursor()
method.
(a) 2 (b) 3 (c) 4 (d) 0
9. To establish the Python-MySQL connection, connect() method is
used with certain parameters or arguments. Which of the following
is not a parameter/argument used with connect () method?
(a) user (b) password (c) database (d) table
10. Choose the correct statement to connect database from Python
code, is host is "localhost", user= "root" the database is “School”
with no password .
(a) connect(host="localhost",user= "root",database =
"School")
(b) connect(host= "localhost",user= "sql",password=NAN,
database = "root")
(c) connect(host= "host",user= "root",password=np.nan,
database = "School")
(d) connect(host= "loca",user="School",password="",
database = "root")
11. It acts as middleware between MYDSQL database connection and
SQL query. (a) cursor (b) Table (c) Query (d) row
12. commit() is required to be used after the execution of certain
queries in Python-MySQL connectivity applications. Identify one
such MySQL command out of the following options:
(a) CREATE (b) INSERT (c) SELECT (d) DROP
13. While working on Python-MySQL connectivity, fetchall() method is
used to get data from table. The method fetchall() returns ?
(a) A list (b) A tuple (c) Tuple of Lists (d) List of Tuples
14. The method cursor() is used in Python-MySQL connectivity
applications. This method is a member of:
(a) sql module (b) pickle module
(c) csv module (d) database-connectivity module
15. To establish a connection between Python and SQL database,
connect () is used. Which of the following arguments may not
necessarily be given while calling
connect() ?
(a) host (b) database (c) user (d) password
16. Ramesh is trying to fetch only one record from result set at a time.
Which method should be used by him?
(a) fetchmany(b) fetchno(c) fetchone(d) fetchall
17. This is the Property of cursor object that returns the number of
rows fetched
a) fetchall() b) resultsetc) rowcountd) none of the above
18. Fill in the blank
data = [('Jane', 'F'),('Joe', 'M'),('John', 'M'),]
stmt = "INSERT INTO employees (first_name, hire_date)
VALUES (%s, %s)"
cursor. (stmt, data)
import mysql.connecotor as c
s=c.connect(host='localhost',user='a',password='a')
cur=c.cursor()
cur.execute('USE TRAVEL')
cur.execute("SELECT * FROM PASSENGERS")
Res=cur.fetchall()
for R in Res:
print(R[1])
s.close()
4. The books table of test database contains the records shown below
and Predict the output of the following code:
Consider the
ENO ENAME DEPT SALARY following
1 ALEX MUSIC 60000 information stored
2 PETER ART 67000 in the table: EMP
3 JOHNY MUSIC 65000
4 USHA ART 85000
**********************************************************