Skip to content

Commit 7b13322

Browse files
committed
Updating repo structure
1 parent 46d5f8a commit 7b13322

File tree

12 files changed

+176
-107
lines changed

12 files changed

+176
-107
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.DS_Store

Northwind_large.sqlite.zip

-10.5 MB
Binary file not shown.

Northwind_populate_data.py

Lines changed: 0 additions & 94 deletions
This file was deleted.

Northwind_small.sqlite

-284 KB
Binary file not shown.

README.md

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,39 @@
1-
# northwind-SQLite3
1+
# Northwind-SQLite3
22

33
This is a version of the Microsoft Access 2000 Northwind sample database, re-engineered for SQLite3.
44

55
The Northwind sample database was provided with Microsoft Access as a tutorial schema for managing small business customers, orders, inventory, purchasing, suppliers, shipping, and employees. Northwind is an excellent tutorial schema for a small-business ERP, with customers, orders, inventory, purchasing, suppliers, shipping, employees, and single-entry accounting.
66

7-
All the TABLES and VIEWS from the MSSQL-2000 version have been converted to Sqlite3 and included here. Also included are two versions prepopulated with data - a small verison and a large version. Should you decide to, you can use the included python script to pump the database full of more data.
7+
All the TABLES and VIEWS from the MSSQL-2000 version have been converted to Sqlite3 and included here. Included is a single version prepopulated with data. Should you decide to, you can use the included python script to pump the database full of more data.
88

9-
![alt tag](https://raw.githubusercontent.com/jpwhite3/northwind-SQLite3/master/Northwind_ERD.png)
9+
[Download here](https://raw.githubusercontent.com/jpwhite3/northwind-SQLite3/master/dist/northwind.db)
10+
11+
# Structure
12+
13+
![alt tag](https://raw.githubusercontent.com/jpwhite3/northwind-SQLite3/master/images/Northwind_ERD.png)
14+
15+
# Build Instructions
16+
17+
## Prerequisites
18+
19+
- You are running in a unix-like environment (Linux, MacOS)
20+
- Python 3.6 or higher (`python3 --version`)
21+
- SQLite3 installed `sqlite3 -help`
22+
23+
## Build
24+
25+
```bash
26+
make build # Creates database at ./dist/northwind.db
27+
```
28+
29+
## Populate with more data
30+
31+
```bash
32+
make populate
33+
```
34+
35+
## Print report of row counts
36+
37+
```bash
38+
make report
39+
```

dist/northwind.db

588 KB
Binary file not shown.
File renamed without changes.

makefile

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
.PHONY: clean build run
2+
.DEFAULT_GOAL := build
3+
4+
5+
bootstrap:
6+
brew install sqlite
7+
brew install graphviz
8+
9+
clean:
10+
rm -rf ./dist
11+
mkdir ./dist
12+
13+
build: clean
14+
sqlite3 dist/northwind.db < src/create.sql > /dev/null
15+
sqlite3 dist/northwind.db < src/update.sql > /dev/null
16+
sqlite3 dist/northwind.db < src/report.sql
17+
18+
populate:
19+
python3 ./src/populate.py
20+
sqlite3 dist/northwind.db < src/report.sql
21+
22+
report:
23+
sqlite3 dist/northwind.db < src/report.sql

Northwind.Sqlite3.create.sql renamed to src/create.sql

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,10 @@
1-
-- Valon Hoti @ 2010-07-04 [YYYY-MM-DD]
2-
-- Prishtine,10000
3-
-- KOSOVE
41
-- DATABASE : Northwind
52
-- ORIGIN : MS SQL
63
-- SOURCE : SQLITE 3
74
--
85
-- Converted TABLES and VIEWS from MS SQL 2000 to Sqlite 3
96
--
107

11-
-- Modified by Len Boyette @ 2013-02-08
12-
-- Added foreign keys
138
PRAGMA foreign_keys=off;
149

1510
-- Categories

src/populate.py

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
from datetime import timedelta, datetime
2+
from random import randint
3+
from random import choice as rc
4+
import sqlite3
5+
6+
7+
# This function will return a random datetime between two datetime objects.
8+
def random_date(start, end):
9+
return start + timedelta(seconds=randint(0, int((end - start).total_seconds())))
10+
11+
12+
# Connect to the DB
13+
conn = sqlite3.connect("./dist/northwind.db")
14+
c = conn.cursor()
15+
16+
# ShipName, ShipAddress, ShipCity, ShipRegion, ShipPostalCode
17+
c.execute(
18+
"select distinct ShipName, ShipAddress, ShipCity, ShipRegion, ShipPostalCode, ShipCountry from [Orders]"
19+
)
20+
locations = [(row[0], row[1], row[2], row[3], row[4], row[5]) for row in c.fetchall()]
21+
22+
# Customer.Id
23+
c.execute("select distinct EmployeeId from [Employees]")
24+
employees = [row[0] for row in c.fetchall()]
25+
26+
# Shipper.Id
27+
c.execute("select distinct ShipperId from [Shippers]")
28+
shippers = [row[0] for row in c.fetchall()]
29+
30+
# Customer.Id
31+
c.execute("select distinct CustomerId from [Customers]")
32+
customers = [row[0] for row in c.fetchall()]
33+
34+
# Create a bunch of new orders
35+
for i in range(randint(15000, 16000)):
36+
sql = "INSERT INTO [Orders] (CustomerId, EmployeeId, OrderDate, RequiredDate, ShippedDate, ShipVia, Freight, ShipName, ShipAddress, ShipCity, ShipRegion, ShipPostalCode, ShipCountry) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"
37+
location = rc(locations)
38+
order_date = random_date(
39+
datetime.strptime("2012-07-10", "%Y-%m-%d"), datetime.today()
40+
)
41+
required_date = random_date(
42+
order_date, order_date + timedelta(days=randint(14, 60))
43+
)
44+
shipped_date = random_date(order_date, order_date + timedelta(days=randint(1, 30)))
45+
params = (
46+
rc(customers), # CustomerId
47+
rc(employees), # EmployeeId
48+
order_date, # OrderDate
49+
required_date, # RequiredDate
50+
shipped_date, # ShippedDate
51+
rc(shippers), # ShipVia
52+
0.00, # Freight
53+
location[0], # ShipName
54+
location[1], # ShipAddress
55+
location[2], # ShipCity
56+
location[3], # ShipRegion
57+
location[4], # ShipPostalCode
58+
location[5], # ShipCountry
59+
)
60+
c.execute(sql, params)
61+
62+
63+
# Product.Id
64+
c.execute("select distinct ProductId, UnitPrice from [Products]")
65+
products = [(row[0], row[1]) for row in c.fetchall()]
66+
67+
# Order.Id
68+
c.execute("select distinct OrderId from [Orders] where Freight = 0.00")
69+
orders = [row[0] for row in c.fetchall()]
70+
71+
# Fill the order with items
72+
for order in orders:
73+
used = []
74+
for x in range(randint(1, len(products))):
75+
sql = "INSERT INTO [Order Details] (OrderId, ProductId, UnitPrice, Quantity, Discount) VALUES (?, ?, ?, ?, ?)"
76+
control = 1
77+
while control:
78+
product = rc(products)
79+
if product not in used:
80+
used.append(product)
81+
control = 0
82+
params = (
83+
# "%s/%s" % (order, product[0]),
84+
order, # OrderId
85+
product[0], # ProductId
86+
product[1], # UnitPrice
87+
randint(1, 50), # Quantity
88+
0, # Discount
89+
)
90+
c.execute(sql, params)
91+
92+
# Cleanup
93+
# c.execute('update [Order] set OrderDate = date(OrderDate), RequiredDate = date(RequiredDate), ShippedDate = date(ShippedDate)')
94+
c.execute("select sum(Quantity)*0.25+10, OrderId from [Order Details] group by OrderId")
95+
orders = [(row[0], row[1]) for row in c.fetchall()]
96+
for order in orders:
97+
c.execute("update [Orders] set Freight=? where OrderId=?", (order[0], order[1]))
98+
99+
100+
conn.commit()
101+
conn.close()

0 commit comments

Comments
 (0)