|
| 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