1
S. No. Question Page
No.
1 Q26. Write a Python program to change the Quantity of the 3-4
product to 91 whose Item_code is 208 in the product_inventory
table of the WarehouseDB database.
2 Q27. Write a Python function AddAndDisplay() to input details 5-7
of an item and store it in the STATIONERY table of ITEMDB
database, and then display all records where Price > 120.
3 Q28. Write a Python function ChangeStatus() to update the Status 8-9
of medicines to 'DISCARD' whose Expiry date is before '2022-
12-31' in the MEDICINES table of PHARMACY database.
4 Q29. Write a Python program to insert a new record into the 10-11
Bank_Account table of Bank database, accepting Accno, Cname,
Atype, and Amount from the user.
5 Q30. Write a Python program to display all records from the 12-13
Bookshop table of the Bstore database.
2
Program26. MySQL database named WarehouseDB has a product_inventory
table in MySQL which contains the following attributes:
• Item_code: Item code (Integer)
• Product_name: Name of product (String)
• Quantity: Quantity of product (Integer)
• Cost: Cost of product (Integer)
Consider the following details to establish Python-MySQL connectivity:
• Username: admin_user
• Password: warehouse2024
• Host: localhost
Write a Python program to change the Quantity of the product to 91 whose
Item_code is 208 in the product_inventory table.
Source Code:
import [Link]
con = [Link](
host="localhost",
user="admin_user",
password="warehouse2024",
database="WarehouseDB"
)
cur = [Link]()
query = "UPDATE product_inventory SET Quantity = 91 WHERE Item_code =
208;"
[Link](query)
[Link]()
3
print("Record updated successfully!")
[Link]()
[Link]()
Output:
4
Program27. A table, named STATIONERY, in ITEMDB database, has the
following structure:
Write the following Python function to perform the specified operation:
AddAndDisplay(): To input details of an item and store it in the table
STATIONERY.
The function should then retrieve and display all records from the
STATIONERY table where the Price is greater than 120.
Assume the following for Python-Database connectivity: Host: localhost,
User: root, Password: Pencil
Source Code:
import [Link]
def AddAndDisplay():
con = [Link](
host="localhost",
user="root",
password="Pencil",
database="ITEMDB"
)
cur = [Link]()
ino = int(input("Enter Item Number: "))
name = input("Enter Item Name: ")
price = float(input("Enter Item Price: "))
5
qty = int(input("Enter Quantity: "))
insert_query = "INSERT INTO STATIONERY VALUES (%s, %s, %s, %s)"
data = (ino, name, price, qty)
[Link](insert_query, data)
[Link]()
print("\nRecord inserted successfully!\n")
print("Items having Price greater than 120:\n")
[Link]("SELECT * FROM STATIONERY WHERE price > 120;")
records = [Link]()
for row in records:
print(row)
[Link]()
[Link]()
AddAndDisplay()
Output:
6
7
Program28: Nutan Kumar is using Python connectivity with MySQL for
maintaining data for a table named MEDICINES in a database PHARMACY.
The table has the following attributes :
• MId (Medicine number) – string
• Mname (Medicine Name) – string
• Expiry (Expiry Date) – Date
• Status (Active/Discard) – string
Consider the following to establish connectivity between Python and
MySQL :
• Username – root
• Password – tiger
• Host – localhost
Help Nutan to write the definition of a user-defined function named
ChangeStatus()in Python to change the Status of the Medicines whose Expiry
is before '2022-12-31' as 'DISCARD'
Source Code:
import [Link]
def ChangeStatus():
con = [Link](
host="localhost",
user="root",
password="tiger",
database="PHARMACY"
)
cur = [Link]()
query = "UPDATE MEDICINES SET Status = 'DISCARD' WHERE Expiry <
'2022-12-31';"
[Link](query)
8
[Link]()
print("Status updated successfully for expired medicines!")
[Link]()
[Link]()
ChangeStatus()
Output:
9
Program29. 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
Note the following to establish connectivity between Python and
MySQL :
· Username – admin
· Password – root
· 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.
Source Code:
import [Link]
con = [Link](
host="localhost",
user="admin",
password="root",
database="Bank"
)
cur = [Link]()
accno = int(input("Enter Account Number: "))
cname = input("Enter Customer Name: ")
atype = input("Enter Account Type: ")
amount = float(input("Enter Amount: "))
query = "INSERT INTO Bank_Account VALUES (%s, %s, %s, %s)"
data = (accno, cname, atype, amount)
[Link](query, data)
[Link]()
print("Record inserted successfully!")
[Link]()
[Link]()
10
Output:
11
Program30. 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:
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 : 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.
Source Code:
import [Link]
con = [Link](
host="localhost",
user="shop",
password="Book",
database="Bstore"
)
cur = [Link]()
query = "SELECT * FROM Bookshop;"
[Link](query)
B_Details = [Link]()
print("Bookshop Table Records:\n")
for row in B_Details:
print(row)
[Link]()
[Link]()
12
Output:
13