0% found this document useful (0 votes)
65 views5 pages

DB Connectivity Board

The document provides various SQL and Python code snippets related to database operations, including selecting, updating, and deleting records from different tables in MySQL. It includes examples of how to establish connections, execute queries, and handle results using Python. Additionally, it poses questions regarding the expected outputs of the code and the functionality of specific methods like fetchone().
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
65 views5 pages

DB Connectivity Board

The document provides various SQL and Python code snippets related to database operations, including selecting, updating, and deleting records from different tables in MySQL. It includes examples of how to establish connections, execute queries, and handle results using Python. Additionally, it poses questions regarding the expected outputs of the code and the functionality of specific methods like fetchone().
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

1.

For the following SQL Table named PASSENGERS in a database TRAVEL:


TNO NAME START END
T1 RAVI KUMAR DELHI MUMBAI
T2 NISHANT JAIN DELHI KOLKATA
T3 DEEPAK PRAKASH MUMBAI PUNE
A cursor named Cur is created in Python for a connection of a host which contains the database
TRAVEL. Write the output for the execution of the following Python statements for the above
SQL Table PASSENGERS: [2]
Cur.execute(‘USE TRAVEL’)

AR
Cur.execute(‘SELECT * FROM PASSENGERS’)
Recs=Cur.fetchall()
for R in Recs:
print(R[1])
cmpt-2021

CS M
2.

T- KU
PG A
DR
R EN
NA

[2022]
3. Consider the following table EMPLOYEE in a Database COMPANY :
Table : EMPLOYEE
E_ID NAME DEPT
H1001 Avneet AC
A1002 Rakesh HR
A1003 Amina AC
H1002 Simon HR
A1004 Pratik AC
Assume that the required library for establishing the connection between Python and MySQL is already
imported in the given Python code. Also assume that DB is the name of the database connection for the
given table EMPLOYEE stored in the database COMPANY.
Predict the output of the following Python code : 2
CUR=DB.cursor()
CUR.execute("USE COMPANY")
CUR.execute("SELECT * FROM EMPLOYEE WHERE DEPT = 'AC' ")
for i in range(2) :
R=CUR.fetchone()
print(R[0], R[1], sep ="#")
[COMPT 2022]
4.

AR
CS M
T- KU
PG A
DR
R EN
NA

[2023]
5.

AR
CS M
T- KU
PG A
DR
R EN

[2023]
NA

6. The table Bookshop in MySQL contains the following attributes :


B_code- Integer
B_name- String
Qty -Integer
Price- Integer
Note the following to establish connectivity between Python and MySQL on a ‘localhost’
• Username is shop
• Password is Book
• The table exists in a MySQL database named Bstore.
The code given below updates the records from the table Bookshop in MySQL. 3
Statement 1 to form the cursor object.
Statement 2 to execute the query that updates the Qty to 20 of the records whose B_code is 105 in
the table.
Statement 3 to make the changes permanent in the database.
import mysql.connector as mysql
def update_book():
mydb=mysql.connect(host="localhost", user="shop",passwd="Book",database="Bstore")
mycursor=__________ # Statement 1
qry= "update Bookshop set Qty=20 where B_code=105"
___________________ # Statement 2
___________________ # Statement 3
[compt 2023]

AR
7. The table Bookshop in MySQL contains the following attributes :
B_code- Integer
B_name- String
Qty -Integer
Price- Integer

CS M
Note the following to establish connectivity between Python and MySQL on a ‘localhost’
• Username is shop
• Password is Book

T- KU
• The table exists in a MySQL database named Bstore.
The code given below updates the records from the table Bookshop in MySQL. 3
Statement 1 to form the cursor object.
Statement 2 to write the query to display all the records from the table.
Statement 3 to read the complete result of the query into the object named B_Details, from the table
Bookshop in the database.
import mysql.connector as mysql
PG A
def Display_book():
mydb=mysql.connect(host="localhost",user="shop",passwd="Book",database="Bstore")
mycursor=___________ # Statement 1
DR

mycursor.execute("_________") # Statement 2
B_Details=__________ # Statement 3
for i in B_Details:
print(i)
[compt 2023]
EN

8.
R
NA

2024
9.

AR
CS M
T- KU
2024
10. Sangeeta wants to write a program in Python to delete the record of a candidate “Raman” from
the table named Placement in MySQL database, Agency:
The table Placement in MySQL contains the following attributes :
CName – String
Dept – String
PG A
Place – String
Salary – integer
Note the following to establish connectivity between Python and MySQL :
DR

Username – root
Password – job
Host – localhost
Help Sangeeta to write the program in Python for the above mentioned task.
Compt 2024
EN

11. Rahim wants to write a program in Python to insert the following record in the table named
Bank_Account in MySQL database, Bank :
Accno – integer
Cname – string
Atype – string
Amount – float
R

Note the following to establish connectivity between Python and MySQL :


Username – admin
Password – root
NA

Host – localhost
The values of fields Accno, Cname, Atype and Amount have to be accepted from the user. Help
Rahim to write the program in Python. Compt 2024

13. fetchone() method fetches only one row in a ResultSet and returns a __________.
(a) Tuple
(b) List
(c) Dictionary
(d) String

You might also like