Computer Science - Class 12
Chapter 15 - Interface of Python with SQL Database

Consider the following code:

importMySQLdb

d b = M y S Q L d b. c o n n e c t (‘ l o c a l h o s t ’ ,

‘LIBMAN’,‘Admin@pwd’,‘library’)

cursor=db.cursor(prepared=TRUE)

sql = “””Insert into table(‘MemId’,

‘MemName’,‘MemAdd’,)values(‘%s’,‘%s’,‘%s’, )”””

insert_value=[(‘1021’, ‘Ronit’, ’12, Block A, Kaveri

Vihar’),

(‘1022’, ‘Smriti’, ‘12/9 Indira Puram’)]

try:

cursor.executemany(sql,insert_value)

print(cursor.rowcount,“Recordsinserted”)

db.commit()

except:

db.rollback()

cursor.close()

db.close()

Question 1-Consider the following code - Teachoo.JPG

Question 1(i)

What of the following statement is connecting database server?

(A) importMySQLdb

(B) MySQLdb.connect

(C) db.cursor

(D) None of these

 

Answer:

Question 1(i)-What of the following - Teachoo.JPG

  • (A) importMySQLdb - Imports the required python module
  • (B) MySQLdb.connect - Connects the MySQL server to the python application   
  • (C) db.cursor - Creates the cursor object which is used for executing the queries
  • (D) None of these 

From the options given above, MySQLdb.connect creates a connection to the MySQL server.

So, the correct answer is (B)

 

Question 1(ii)

The role of the statement ‘cursor=db.cursor’ is:

(A) Create an instance of a cursor

(B) Moves cursor row wise

(C) Connects cursor to database

(D) Prepares cursor to move

 

Answer:

 

The statement ‘cursor=db.cursor’ is used to create an object ‘db’ of the cursor class.

Checking the options

  • (A) Create an instance of a cursor - It is a role of the given statement
  • (B) Moves cursor row wise -  It is not a role of the given statement
  • (C) Connects cursor to database -  It is not a role of the given statement   
  • (D) Prepares cursor to move -  It is not a role of the given statement

So, the correct answer is (A)

 

Question 1(iii)

Which of the following statement will add the record in the table?

(A) Insert Into

(B) Insert Value

(C) cursor.executemany(sql,insert_value)

(D) None of these

 

Answer:

The executemany( ) function is used to insert multiple rows into a table.

Syntax: cursorobject.executemany(sql_statemnt,arguments)

 

Checking the options

  • (A) Insert Into - Does not add records to a table in python  
  • (B) Insert Value - Does not add records to a table in python
  • (C) cursor.executemany(sql,insert_value) - The statement is used to add multiple records to a table in MySQL database within python.
  • (D) None of these 

So, the correct answer is (C)

 

Question 1(iv)

What will be the output of following statement in the given code? print(cursor.rowcount,“Recordsinserted”)

(A) 0

(B) 1

(C) 2

(D) 3

 

Answer:

The statement print(cursor.rowcount,“Recordsinserted”) will display the number of records affected by the query . Here, the query is to add 2 new rows to a table . So, the number of records affected will be 2 .   

 

Checking the options

  • (A) 0 - It is not the correct output.
  • (B) 1 - It is not the correct output.   
  • (C) 2 - It is the Correct output. 
  • (D) 3 - It is not the correct output.  

So, the correct answer is (C)

 

Question 1(v)

What will be the result of ‘cursor.close()’?

(A) Close the cursor

(B) Reset all results

(C) Deletes all references to the connection

(D) All of these

 

Answer:

The statement cursor.close( ), closes the cursor, resets all results, and ensures that the cursor object has no reference to its original connection object .

 

Checking the options

  • (A) Close the cursor - It is a result of cursor.close( )
  • (B) Reset all results - It is a result of cursor.close( )   
  • (C) Deletes all references to the connection - It is a result of cursor.close( )   
  • (D) All of these   

In the options given above, all the options given are results of executing cursor.close( ).

So, the correct answer is (D)

Learn in your speed, with individual attention - Teachoo Maths 1-on-1 Class


Transcript

Case Base Question 1 Consider the following code: importMySQLdb d b = M y S Q L d b. c o n n e c t (‘l o c a l h o s t’ , ‘LIBMAN’,‘Admin@pwd’,‘library’) cursor=db.cursor(prepared=TRUE) sql = “””Insert into table(‘MemId’, ‘MemName’,‘MemAdd’,)values(‘%s’,‘%s’,‘%s’, )””” insert_value=[(‘1021’, ‘Ronit’, ’12, Block A, Kaveri Vihar’), (‘1022’, ‘Smriti’, ‘12/9 Indira Puram’)] try: cursor.executemany(sql,insert_value) print(cursor.rowcount,“Recordsinserted”) db.commit() except: db.rollback() cursor.close() db.close() Question 1 (i) What of the following statement is connecting database server? (A) importMySQLdb (B) MySQLdb.connect (C) db.cursor (D) None of these Imports the required python module creating the cursor object for executing the queries The values to be inserted is assigned to the variable inserted_value Prints the number of rows affected by the query Reverts the last changes made to the database Closes the connection to the database Creates a connection between MySQL database & Python application The query to be executed is assigned to the variable sql Inserts multiple records to the table Confirms the changes made by the user to the database Closes the cursor and resets all the results

Ask a doubt
Davneet Singh's photo - Co-founder, Teachoo

Made by

Davneet Singh

Davneet Singh has done his B.Tech from Indian Institute of Technology, Kanpur. He has been teaching from the past 13 years. He provides courses for Maths, Science, Social Science, Physics, Chemistry, Computer Science at Teachoo.