-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate_database.py
More file actions
134 lines (113 loc) · 4.84 KB
/
Copy pathcreate_database.py
File metadata and controls
134 lines (113 loc) · 4.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
"""
create_database.py
-------------------
Generates a sample SQLite database (shop.db) with three related tables:
customers - who bought things
products - what was sold
orders - the transactions linking customers to products
Run this once before starting the app:
python create_database.py
"""
import sqlite3
import random
from datetime import datetime, timedelta
DB_PATH = "shop.db"
# ---- Sample data pools -----------------------------------------------------
FIRST_NAMES = ["Aarav", "Vivaan", "Diya", "Ananya", "Ishaan", "Priya", "Rohan",
"Kavya", "Arjun", "Meera", "Karan", "Neha", "Sanya", "Aditya",
"Riya", "Vikram", "Pooja", "Rahul", "Tanvi", "Yash"]
LAST_NAMES = ["Sharma", "Verma", "Gupta", "Patel", "Kumar", "Singh", "Reddy",
"Nair", "Iyer", "Das", "Mehta", "Joshi", "Rao", "Malhotra"]
CITIES = ["Delhi", "Mumbai", "Bangalore", "Chennai", "Hyderabad", "Pune",
"Kolkata", "Ahmedabad", "Jaipur", "Lucknow"]
CATEGORIES = ["Electronics", "Clothing", "Home & Kitchen", "Books",
"Sports", "Beauty", "Toys", "Groceries"]
PRODUCT_NAMES = {
"Electronics": ["Wireless Earbuds", "Smart Watch", "Bluetooth Speaker",
"Power Bank", "Laptop Stand", "USB-C Hub"],
"Clothing": ["Cotton T-Shirt", "Denim Jacket", "Running Shoes",
"Formal Shirt", "Hoodie", "Sneakers"],
"Home & Kitchen": ["Non-stick Pan", "Air Fryer", "Coffee Maker",
"Blender", "Table Lamp", "Storage Boxes"],
"Books": ["Novel - Fiction", "Self Help Guide", "Cookbook",
"Biography", "Sci-Fi Novel", "Business Book"],
"Sports": ["Yoga Mat", "Dumbbell Set", "Cricket Bat", "Football",
"Resistance Bands", "Water Bottle"],
"Beauty": ["Face Cream", "Shampoo", "Perfume", "Lipstick",
"Sunscreen", "Hair Serum"],
"Toys": ["Building Blocks", "Remote Car", "Puzzle Set",
"Action Figure", "Board Game", "Soft Toy"],
"Groceries": ["Basmati Rice 5kg", "Olive Oil 1L", "Almonds 500g",
"Green Tea Box", "Honey 500g", "Whole Wheat Atta 5kg"],
}
def build_database():
conn = sqlite3.connect(DB_PATH)
cur = conn.cursor()
# Start fresh every time this script runs
cur.executescript("""
DROP TABLE IF EXISTS orders;
DROP TABLE IF EXISTS products;
DROP TABLE IF EXISTS customers;
CREATE TABLE customers (
customer_id INTEGER PRIMARY KEY,
customer_name TEXT NOT NULL,
city TEXT,
signup_date TEXT
);
CREATE TABLE products (
product_id INTEGER PRIMARY KEY,
product_name TEXT NOT NULL,
category TEXT,
price REAL
);
CREATE TABLE orders (
order_id INTEGER PRIMARY KEY,
customer_id INTEGER,
product_id INTEGER,
quantity INTEGER,
order_date TEXT,
sales REAL,
FOREIGN KEY (customer_id) REFERENCES customers(customer_id),
FOREIGN KEY (product_id) REFERENCES products(product_id)
);
""")
random.seed(42) # reproducible sample data
# ---- customers ----
customers = []
for i in range(1, 61): # 60 customers
name = f"{random.choice(FIRST_NAMES)} {random.choice(LAST_NAMES)}"
city = random.choice(CITIES)
signup = datetime(2024, 1, 1) + timedelta(days=random.randint(0, 500))
customers.append((i, name, city, signup.strftime("%Y-%m-%d")))
cur.executemany("INSERT INTO customers VALUES (?,?,?,?)", customers)
# ---- products ----
products = []
pid = 1
for category, names in PRODUCT_NAMES.items():
for name in names:
price = round(random.uniform(150, 5000), 2)
products.append((pid, name, category, price))
pid += 1
cur.executemany("INSERT INTO products VALUES (?,?,?,?)", products)
# ---- orders (the transactional table) ----
orders = []
order_id = 1
for _ in range(800): # 800 orders
customer_id = random.randint(1, 60)
product = random.choice(products)
product_id, _, _, price = product
quantity = random.randint(1, 5)
order_date = datetime(2024, 1, 1) + timedelta(days=random.randint(0, 550))
sales = round(price * quantity, 2)
orders.append((order_id, customer_id, product_id, quantity,
order_date.strftime("%Y-%m-%d"), sales))
order_id += 1
cur.executemany("INSERT INTO orders VALUES (?,?,?,?,?,?)", orders)
conn.commit()
conn.close()
print(f"✅ Database created: {DB_PATH}")
print(f" customers: {len(customers)} rows")
print(f" products: {len(products)} rows")
print(f" orders: {len(orders)} rows")
if __name__ == "__main__":
build_database()