Create a relational database that has a product table, a customer table, an order table, and an order item table. In the product table, store an id, name, picture, description, and price. In the customer table, store the id, name, and address. In the order table, store the order id and customer id. In the order item table, store the order id, product id, and quantity. Write a function to let you find all orders for a given customer. Write a function to let you find all orders with a total cost greater than some specified value.
The commands to set up the tables for this answer are as follows:
```
con.execute("create table Product (id INT, name VARCHAR(50), picture
VARCHAR(100), description VARCHAR(500), price INT)")
con.execute("create table Customer (id INT, name VARCHAR(50), address
VARCHAR(100))")
con.execute("create table Order (id INT, customerid INT)")
con.execute("create table OrderItem (orderid INT, productid INT, quantity INT)")
```
The function to let you find all orders for a given customer is as follows (assuming you are given customer name, if given customer id you can skip the first step):
```
The function to let you find all orders for a given customer is as follows (assuming you
are given customer name, if given customer id you can skip the first step):
"com.mysql.jdbc.Driver")
con = db.cursor()
#Read info from the database to find the customer id
con.execute("select id from Customer where name=="+str(customerName))
customerId
for i in range(0, con.rowcount):
results= con.fetchone()
customerId = (results[0])
#Read info from database to find the orderIds stored for this customerid
orderIds = []
con.execute("select id from Order where customerid=="+str(customerId))
for i in range(0, con.rowcount):
results= con.fetchone()
orderIds.append(results[0])
return orderIds
```
The function to let you find all orders with a total cost greater than some specified value is as follows:
Note: This requires a number of steps, as one can’t assume that each order only has a single entry in the OrderItem table.
```
from com.ziclix.python.sql import zxJDBC
from csv import *
#Returns a list of filenames for all pictures of people above the variable age
def findAllOrdersByCost(cost):
#Grab the databse
db =zxJDBC.connect("jdbc:mysql://localhost/test", "root", None ,
"com.mysql.jdbc.Driver")
con = db.cursor()
#Read info from database to find all orderIds
orderIds = []
con.execute("select id from Order")
for i in range(0, con.rowcount):
results= con.fetchone()
orderIds.append(results[0])
#Create a dictionary to store each order's information
orderInformation ={}
#Fill the dictionary with empty lists at first
for id in orderIds:
orderInformation[id] = []
#Read info from OrderItem table to fill in orderIds dictionary
con.execute("select * from OrderItem")
for i in range(0, con.rowcount):
results= con.fetchone()
#Store as a list in case each order has multiple products
productId = results[1]
quantity = results[2]
orderInformation[results[0]].append([productId, quantity])
orderIdsAboveCost = []
#Iterate through order ids to sum up cost
for id in orderIds:
orderCost = 0
for productOrder in orderInformation:
#Grab the products cost
productCost = 0
con.execute("select price from Product where id=="+str(productOrder[0]))
for i in range(0, con.rowcount):
results= con.fetchone()
productCost = int(results[0])
orderCost += productCost*int(productOrder[1])
if orderCost>cost:
orderIdsAboveCost.append(id)
return orderIdsAboveCost
```
You might also like to view...
Operating systems that have windows and icons have what type of user interface?
a. command-driven b. graphical user c. menu-driven d. magnetic tape–based
Current accessibility guidelines state that you should provide text alternatives for any nontext content in Web pages. List at least three related, special instructions on this point.
What will be an ideal response?