Question: WRITE THE CODE FOR MYSQL WITH PYTHON AND ANSWER THESE QUESTIONS BY USING THE GIVEN CODE BELOW PLEASE! Q: Print vendor information who sold unpopular

WRITE THE CODE FOR MYSQL WITH PYTHON

AND ANSWER THESE QUESTIONS BY USING THE GIVEN CODE BELOW PLEASE!

Q: Print vendor information who sold unpopular items.

Q: Define a function that reports the total price of the items with respect to their type. For example, the total price of sold paintings is 420000.

import mysql.connector

# Connect to the MySQL server

cnx = mysql.connector.connect(host="localhost", user="root",

password="Yurtdisi4")

cursor = cnx.cursor()

# Create the 'Market' database

cursor.execute('CREATE DATABASE Market;')

# Use the 'Market' database

cursor.execute('USE Market;')

cursor = cnx.cursor(buffered=True)

# Create the 'Vendor' table

cursor.execute('CREATE TABLE Vendor (vendor_id INTEGER PRIMARY KEY, vendor_name VARCHAR(255), license_start DATE, license_end DATE);')

# Create the 'Item' table

cursor.execute('CREATE TABLE Item (item_id INTEGER PRIMARY KEY, item_name VARCHAR(255), item_type VARCHAR(255), previous_price FLOAT, last_sold_date DATE);')

# Create the 'Auction' table

cursor.execute('CREATE TABLE Auction (auction_id INTEGER PRIMARY KEY, vendor_id INTEGER, item_id INTEGER, price FLOAT, FOREIGN KEY (vendor_id) REFERENCES Vendor(vendor_id), FOREIGN KEY (item_id) REFERENCES Item(item_id));')

sql1 = "INSERT INTO Vendor (vendor_id, vendor_name, license_start, license_end) VALUES (%s, %s, %s, %s)"

val1 = [(1, 'John Smith', '2021-01-01', '2022-12-31'),

(2, 'Melinda Jones', '2021-02-01', '2022-11-30'),

(3, 'Tom Wilson', '2021-03-01', '2022-10-31'),

(4, 'Rachel Williams', '2021-05-01', '2022-09-30'),

(5, 'Adam Johnson', '2021-06-01', '2022-08-31')]

cursor.executemany(sql1, val1)

cnx.commit()

print(cursor.rowcount, "was inserted.")

# We have inserted the tables here

sql2 = "INSERT INTO Item (item_id, item_name, item_type, previous_price, last_sold_date) VALUES (%s, %s, %s, %s, %s)"

val2 = [(11, 'Ball', 'Bronze', 15.50, '2022-12-31'),

(22, 'Jewelery', 'Diamond', 30000.0, '2020-01-23'),

(33, 'Watch', 'Gold', 200.0, '2021-11-30'),

(44, 'Medal', 'Silver', 120.0, '2012-10-06'),

(55, 'Wire', 'Copper', 5.0, '2022-12-15')]

cursor.executemany(sql2, val2)

cnx.commit()

print(cursor.rowcount, "was inserted.")

sql3 = "INSERT INTO Auction (auction_id, vendor_id, item_id, price) VALUES (%s, %s, %s, %s)"

val3 = [(111, 1, 11, 10.0),

(222, 2, 22, 20.0),

(333, 3, 33, 30.0),

(444, 4, 44, 40.0),

(555, 5, 55, 50.0)]

cursor.executemany(sql3, val3)

cnx.commit()

print(cursor.rowcount, "was inserted.")

cursor.execute("SHOW TABLES")

# Find the most expensive item

expensive_query = "SELECT item_name FROM Item WHERE previous_price = (SELECT MAX(previous_price) FROM Item)"

cursor.execute(expensive_query)

most_expensive = cursor.fetchone()[0]

print("The most expensive item is:", most_expensive)

# Find the most popular item

popular_query = "SELECT item_name FROM Item WHERE last_sold_date = (SELECT MAX(last_sold_date) FROM Item)"

cursor.execute(popular_query)

most_popular = cursor.fetchone()[0]

print("The most popular item is:", most_popular)

# Find the vendor with the most auctions

vendor_query = "SELECT vendor_name, COUNT(*) as num_auctions FROM Auction INNER JOIN Vendor ON Auction.vendor_id = Vendor.vendor_id GROUP BY vendor_name ORDER BY num_auctions DESC LIMIT 1"

cursor.execute(vendor_query)

most_active_vendor = cursor.fetchone()[0]

print("The vendor with the most auctions is:", most_active_vendor)

#cursor.execute(expensive)

#expensive = cursor.fetchall()

myresult = cursor.fetchall()

#We show their tables

cursor.execute('SELECT * FROM Vendor');

cursor.execute('SELECT vendor_id FROM Auction');

#PRICES

cursor.execute('SELECT price FROM Auction');

cursor.execute('SELECT * FROM Item');

cursor.execute('SELECT * FROM Auction');

def report_total_price_by_type(mysql_config, item_type):

# Get the total price of items with the specified type

cursor.execute(

"SELECT SUM(a.price) FROM AUCTION a INNER JOIN ITEM i ON a.item_id = i.item_id WHERE i.item_type = %s",

(item_type,))

total_price = cursor.fetchone()[0]

# Close the connection to the database

cursor.close()

return total_price

total_price_of_paintings = report_total_price_by_type(cnx, "painting")

print(f"The total price of sold paintings is {total_price_of_paintings}")

#It gives the info

for x in myresult:

print(x)

# Close the connection to the MySQL server

cnx.close()

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!