Test Index

Interface of Python with SQL Database Get it on Google PlayDownload on the App Store CBSE Board Videos

© examsnet.com
Question : 9 of 10
Marks: +1, -0
(a) (i) What possible output(s) are expected to be displayed on screen at the time of execution of the following code ? (2)
import random
S=["Pen","Pencil","Eraser","Bag","Book"]
for i in range (1,2):
f=random.randint(i,3)
s=random.randint(i+1,4)
print(S[f],S[s],sep=":")
Options :
(I) Pencil:Book
(II) Pencil:Book
Eraser:Bag
(III) Pen:Book
Bag:Book
(IV) Bag:Eraser
(ii) 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
OR
(b) (i) Predict the output of the code given below : (2)
text="LearningCS"
L=len(text)
ntext=""
for i in range (0,L):
if text[i].islower():
ntext=ntext+text[i].upper()
elif text [i].isalnum():
ntext=ntext+text[i-1]
else:
ntext=ntext+'&&'
print(ntext)
(ii) 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 reads the records from the table Bookshop and displays all the records : (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
def Display_book():
mydb=mysql.connect(host="localhost",
user="shop",passwd="Book",database="Bstore")
mycursor=___________ # Statement 1
mycursor.execute("_________") # Statement 2
B_Details=__________ # Statement 3
for i in B_Details:
print(i)
[CBSE 2023 Compartment]
Solution:  
(a)(i)
range(1,2) gives only i = 1, so the loop runs once and only one line is printed.
f = random.randint(1,3): f is 1, 2 or 3, so S[f] is Pencil, Eraser or Bag.
s = random.randint(2,4): s is 2, 3 or 4, so S[s] is Eraser, Bag or Book.
(I) Pencil:Book: f = 1, s = 4. Possible.
(II) and (III) have two lines, so they are not possible ((III) also needs Pen, i.e. f = 0).
(IV) Bag:Eraser: f = 3, s = 2. Possible.
Possible outputs: (I) and (IV).
(a)(ii)
Statement 1: mycursor=mydb.cursor()
Statement 2: mycursor.execute(qry)
Statement 3: mydb.commit()
cursor() creates the cursor object from the connection, execute() runs the UPDATE query and commit() makes the change permanent in the database.
OR (b)(i)
text = "LearningCS", L = 10. A lowercase letter is added in uppercase; any other letter or digit adds the previous character text[i-1].
itext[i]BranchAdded
0Lnot lower, alnum: text[-1]S
1-7e a r n i n glower: upper()EARNING
8Cnot lower, alnum: text[7]g
9Snot lower, alnum: text[8]C
For i = 0, text[-1] is the last character 'S'.
Output:
SEARNINGgC
(b)(ii)
Statement 1: mycursor=mydb.cursor()
Statement 2: mycursor.execute("SELECT * FROM Bookshop")
Statement 3: B_Details=mycursor.fetchall()
fetchall() returns all rows of the result as a list of tuples, which the loop then prints one by one.
© examsnet.com
Advertisements
Go to Question: