Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added Untitled Diagram.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
username =
password =
host =
database =
91 changes: 91 additions & 0 deletions database.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
# sudo pip install SQLAlchemy
# sudo pip install pymysql
import csv
import config
from sqlalchemy import *

metadata = MetaData()
db = create_engine("mysql+pymysql://" + config.username + ":" + config.password + "@" + config.host + "/" + config.database);
connection = db.connect()

#User is inserted only if customerName is not None, and does not already exists in the DB
def populateUser(row):
ID = None
if row['customerName']:
table = Table('User', metadata, autoload=True, autoload_with=db)
sel = select([table.c.userID]).where(table.c.customerName == row['customerName'])
for r in db.execute(sel):
ID = r[0]
if not ID:
ins = table.insert().values(customerName = row['customerName'],
contactLastName = row['contactLastName'],
contactFirstName = row['contactFirstName'],
phone = row['phone']);
res = connection.execute(ins)
ID = res.inserted_primary_key[0]
return ID

#Address is inserted if atleast one field is non None
def populateAddress(row, user_ID):
if row['addressLine1'] or row['addressLine2'] or row['city'] or row['state'] or row['country'] or row['postalCode']:
table = Table('Address', metadata, autoload=True, autoload_with=db)
ins = table.insert().values(userID = user_ID,
addressLine1 = row['addressLine1'],
addressLine2 = row['addressLine2'],
city = row['city'],
state = row['state'],
country = row['country'],
postalCode = row['postalCode']);
connection.execute(ins)

#Product is inserted if atleast one of name or code is not None
def populateProducts(row):
ID = None
if row['productName'] or row['productCode']:
table = Table('Product', metadata, autoload=True, autoload_with=db)
ins = table.insert().values(productName = row['productName'],
productCode = row['productCode'],
productDescription = row['productDescription'],
quantityInStock = row['quantityInStock'],
buyPrice = '1000',
sellPrice = '1000');
res = connection.execute(ins)
ID = res.inserted_primary_key[0]
return ID

#Order is inserted if productID is not None
def populateOrder(row, product_ID, user_ID):
ID = None
if productID:
table = Table('Order', metadata, autoload=True, autoload_with=db)
ins = table.insert().values(userID = user_ID,
orderDate = row['orderDate'],
status = row['status']);
res = connection.execute(ins)
ID = res.inserted_primary_key[0]
return ID

#Order detail is inserted only if orderID is not None
def populateOrderDetail(row, order_ID, product_ID):
if order_ID:
table = Table('OrderDetails', metadata, autoload=True, autoload_with=db)
ins = table.insert().values(orderID = order_ID,
productID = product_ID,
quantity = row['quantityOrdered'],
sellPrice = row['priceEach'],
buyPrice = row['buyPrice']);
connection.execute(ins);
cnt = 0
with open('a3.csv', 'rb') as f:
reader = csv.DictReader(f)
for row in reader:
for col in row:
if row[col] == 'NULL':
row[col] = None
userID = populateUser(row)
populateAddress(row, userID)
productID = populateProducts(row)
orderID = populateOrder(row, productID, userID)
populateOrderDetail(row, orderID, productID)
cnt += 1
print cnt
30 changes: 30 additions & 0 deletions queries.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
1
select orderDate, SUM(sellPrice * quantity) as Sales
FROM Orders INNER JOIN OrderDetails
WHERE Orders.orderID = OrderDetails.orderID
and Orders.status != 'Cancelled'
GROUP BY orderDate

2
select orderDate, SUM((sellPrice - buyPrice) * quantity) as Profit
FROM Orders INNER JOIN OrderDetails
WHERE Orders.orderID = OrderDetails.orderID
and Orders.status != 'Cancelled'
GROUP BY orderDate

3
select Users.userID, customerName, AVG(quantity) as AverageUnitsOrdered
FROM Orders
INNER JOIN OrderDetails
on Orders.orderID = OrderDetails.orderID
INNER JOIN Users
on Users.userID = Orders.userID
GROUP BY Users.userID

4
UPDATE Product
SET isAvailable = '0'
WHERE productID = 2

5
-- (to be implemented using Spring in assignment 4)
261 changes: 261 additions & 0 deletions query_result.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,261 @@
"orderDate","Sales"
"2003-01-06",10195
"2003-01-09",10551
"2003-01-10",5507
"2003-01-29",50205
"2003-01-31",40176
"2003-02-11",53952
"2003-02-17",52149
"2003-02-24",22286
"2003-03-03",50977
"2003-03-10",25844
"2003-03-18",48413
"2003-03-24",7668
"2003-03-25",16534
"2003-03-26",11067
"2003-04-01",33378
"2003-04-04",21662
"2003-04-11",1620
"2003-04-16",44336
"2003-04-21",3096
"2003-04-28",35777
"2003-04-29",45862
"2003-05-07",16698
"2003-05-08",50845
"2003-05-20",14564
"2003-05-21",40178
"2003-05-28",57141
"2003-06-03",58903
"2003-06-06",13888
"2003-06-12",29375
"2003-06-16",23142
"2003-06-25",2880
"2003-06-27",22339
"2003-07-01",23418
"2003-07-02",55638
"2003-07-04",14210
"2003-07-07",32107
"2003-07-10",13940
"2003-07-16",23981
"2003-07-24",38681
"2003-08-01",29696
"2003-08-08",56020
"2003-08-10",41065
"2003-08-13",1120
"2003-08-25",50412
"2003-09-03",6619
"2003-09-05",32655
"2003-09-11",41548
"2003-09-12",29995
"2003-09-19",38284
"2003-09-21",32711
"2003-09-25",9812
"2003-09-28",44977
"2003-10-02",4449
"2003-10-04",24112
"2003-10-05",36787
"2003-10-06",61525
"2003-10-08",4624
"2003-10-09",17744
"2003-10-10",56138
"2003-10-11",20471
"2003-10-17",36134
"2003-10-18",30878
"2003-10-20",22015
"2003-10-21",37147
"2003-10-22",67351
"2003-10-28",50766
"2003-11-04",53713
"2003-11-05",79498
"2003-11-06",99929
"2003-11-07",31467
"2003-11-08",33890
"2003-11-11",42793
"2003-11-12",100076
"2003-11-13",34595
"2003-11-14",122396
"2003-11-15",28259
"2003-11-18",33882
"2003-11-19",10772
"2003-11-20",83413
"2003-11-21",35470
"2003-11-25",75798
"2003-11-26",78625
"2003-11-27",20690
"2003-12-01",48778
"2003-12-02",119105
"2003-12-03",13076
"2003-12-05",36546
"2003-12-09",59339
"2004-01-02",49660
"2004-01-09",21060
"2004-01-12",47161
"2004-01-15",49190
"2004-01-16",59877
"2004-01-22",7299
"2004-01-26",22191
"2004-01-29",36014
"2004-02-02",5762
"2004-02-04",22481
"2004-02-09",7588
"2004-02-10",12610
"2004-02-12",32559
"2004-02-18",16892
"2004-02-19",56788
"2004-02-20",44961
"2004-02-21",18983
"2004-02-22",47451
"2004-02-26",23511
"2004-03-02",40970
"2004-03-10",20331
"2004-03-11",43369
"2004-03-15",33760
"2004-03-19",15309
"2004-03-20",24993
"2004-03-29",7188
"2004-03-30",31669
"2004-04-02",29306
"2004-04-03",5927
"2004-04-05",22624
"2004-04-09",28215
"2004-04-12",16162
"2004-04-13",51238
"2004-04-20",1702
"2004-04-26",6287
"2004-04-29",26160
"2004-05-04",32238
"2004-05-05",63896
"2004-05-08",11852
"2004-05-11",42745
"2004-05-18",31084
"2004-05-26",25087
"2004-06-03",37302
"2004-06-04",4646
"2004-06-08",4699
"2004-06-14",16737
"2004-06-15",66132
"2004-06-17",23028
"2004-06-28",42042
"2004-06-30",18479
"2004-07-02",9402
"2004-07-06",51602
"2004-07-07",20319
"2004-07-12",35074
"2004-07-16",6384
"2004-07-19",35781
"2004-07-20",61248
"2004-07-21",57857
"2004-07-23",47882
"2004-08-02",51260
"2004-08-04",2604
"2004-08-06",33325
"2004-08-09",19971
"2004-08-17",48240
"2004-08-19",39613
"2004-08-20",85599
"2004-08-21",32306
"2004-08-27",43159
"2004-08-28",1976
"2004-08-30",61374
"2004-09-01",38762
"2004-09-03",12527
"2004-09-07",5860
"2004-09-08",84135
"2004-09-09",33928
"2004-09-10",19480
"2004-09-15",31329
"2004-09-16",17368
"2004-09-27",6086
"2004-09-30",34351
"2004-10-06",3486
"2004-10-11",53078
"2004-10-13",47466
"2004-10-14",76062
"2004-10-15",60275
"2004-10-16",97327
"2004-10-21",55559
"2004-10-22",87317
"2004-10-29",19490
"2004-11-01",79933
"2004-11-02",37628
"2004-11-03",44399
"2004-11-04",99181
"2004-11-05",86509
"2004-11-09",19226
"2004-11-10",20547
"2004-11-12",37705
"2004-11-15",49982
"2004-11-16",15832
"2004-11-17",93142
"2004-11-18",26233
"2004-11-19",29521
"2004-11-20",51252
"2004-11-21",25479
"2004-11-22",12070
"2004-11-23",48899
"2004-11-24",125132
"2004-11-25",20535
"2004-11-29",56158
"2004-12-01",39903
"2004-12-02",46467
"2004-12-03",23335
"2004-12-04",65724
"2004-12-07",25541
"2004-12-09",26316
"2004-12-10",84828
"2004-12-15",32602
"2004-12-16",52121
"2004-12-17",31825
"2005-01-05",12678
"2005-01-06",47661
"2005-01-07",8298
"2005-01-10",14408
"2005-01-12",39572
"2005-01-19",13905
"2005-01-20",55321
"2005-01-23",35117
"2005-01-26",33957
"2005-01-31",46752
"2005-02-02",21405
"2005-02-03",49564
"2005-02-08",3465
"2005-02-09",23623
"2005-02-10",48951
"2005-02-16",34442
"2005-02-17",80342
"2005-02-22",36905
"2005-02-23",14153
"2005-02-28",4473
"2005-03-01",46995
"2005-03-02",3520
"2005-03-03",58280
"2005-03-04",55856
"2005-03-09",29846
"2005-03-10",8801
"2005-03-11",33625
"2005-03-15",18068
"2005-03-17",17916
"2005-03-23",27713
"2005-03-28",12404
"2005-03-30",46726
"2005-04-01",62033
"2005-04-03",43589
"2005-04-07",12203
"2005-04-08",78607
"2005-04-14",35099
"2005-04-15",21662
"2005-04-22",52847
"2005-04-23",2332
"2005-04-29",36422
"2005-05-01",29121
"2005-05-03",46890
"2005-05-05",28530
"2005-05-06",50743
"2005-05-09",10940
"2005-05-10",35376
"2005-05-13",28591
"2005-05-16",23644
"2005-05-17",52412
"2005-05-29",49867
"2005-05-30",14392
"2005-05-31",70997
Loading