0 votes
14 views
ago in Computer Science by (8.5k points)

A table, named STATIONERY, in ITEMDB database, has the following structure:

FieldType
itemNoint(11)
itemNamevarchar(15)
pricefloat
qtyint(11)

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

1 Answer

0 votes
ago by (56.5k points)
 
Best answer

def AddAndDisplay():

 import mysql.connector as mycon

 mydb=mycon.connect(host="localhost",user="root",

 passwd="Pencil",database="ITEMDB")

 mycur=mydb.cursor()

 no=int(input("Enter Item Number: "))

 nm=input("Enter Item Name: ")

 pr=float(input("Enter price: "))

 qty=int(input("Enter qty: "))

 query="INSERT INTO stationery VALUES ({},'{}',{},{})"

 query=query.format(no,nm,pr,qty)

 mycur.execute(query)

 mydb.commit()

 mycur.execute("select * from stationery where price>120")

 for rec in mycur:

 print(rec)

...