sidharth-pm commited on
Commit
ff679e0
·
verified ·
1 Parent(s): 4284ad5

Create database.py

Browse files
Files changed (1) hide show
  1. database.py +131 -0
database.py ADDED
@@ -0,0 +1,131 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import sqlite3
2
+ import os
3
+
4
+ DB_PATH = "company.db"
5
+
6
+ def init_database():
7
+ """Initialize the SQLite database with sample tables and data."""
8
+ conn = sqlite3.connect(DB_PATH)
9
+ cursor = conn.cursor()
10
+
11
+ # Create tables
12
+ cursor.executescript("""
13
+ CREATE TABLE IF NOT EXISTS employees (
14
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
15
+ name TEXT NOT NULL,
16
+ department TEXT NOT NULL,
17
+ salary REAL NOT NULL,
18
+ hire_date TEXT NOT NULL,
19
+ manager_id INTEGER,
20
+ email TEXT UNIQUE
21
+ );
22
+
23
+ CREATE TABLE IF NOT EXISTS departments (
24
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
25
+ name TEXT NOT NULL UNIQUE,
26
+ budget REAL NOT NULL,
27
+ location TEXT NOT NULL,
28
+ head_count INTEGER DEFAULT 0
29
+ );
30
+
31
+ CREATE TABLE IF NOT EXISTS projects (
32
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
33
+ title TEXT NOT NULL,
34
+ department TEXT NOT NULL,
35
+ start_date TEXT NOT NULL,
36
+ end_date TEXT,
37
+ status TEXT DEFAULT 'active',
38
+ budget REAL NOT NULL
39
+ );
40
+
41
+ CREATE TABLE IF NOT EXISTS sales (
42
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
43
+ employee_id INTEGER NOT NULL,
44
+ product TEXT NOT NULL,
45
+ amount REAL NOT NULL,
46
+ sale_date TEXT NOT NULL,
47
+ region TEXT NOT NULL,
48
+ FOREIGN KEY (employee_id) REFERENCES employees(id)
49
+ );
50
+ """)
51
+
52
+ # Seed data only if empty
53
+ cursor.execute("SELECT COUNT(*) FROM employees")
54
+ if cursor.fetchone()[0] == 0:
55
+ cursor.executescript("""
56
+ INSERT INTO departments (name, budget, location, head_count) VALUES
57
+ ('Engineering', 2500000, 'San Francisco', 12),
58
+ ('Sales', 1800000, 'New York', 15),
59
+ ('Marketing', 900000, 'Chicago', 8),
60
+ ('HR', 600000, 'Austin', 5),
61
+ ('Finance', 750000, 'Boston', 6);
62
+
63
+ INSERT INTO employees (name, department, salary, hire_date, email) VALUES
64
+ ('Alice Johnson', 'Engineering', 120000, '2020-03-15', 'alice@company.com'),
65
+ ('Bob Smith', 'Engineering', 115000, '2019-07-22', 'bob@company.com'),
66
+ ('Carol White', 'Sales', 85000, '2021-01-10', 'carol@company.com'),
67
+ ('David Brown', 'Sales', 92000, '2018-11-05', 'david@company.com'),
68
+ ('Eve Davis', 'Marketing', 78000, '2022-04-18', 'eve@company.com'),
69
+ ('Frank Miller', 'HR', 72000, '2020-09-30', 'frank@company.com'),
70
+ ('Grace Wilson', 'Engineering', 130000, '2017-06-01', 'grace@company.com'),
71
+ ('Henry Moore', 'Finance', 95000, '2019-02-14', 'henry@company.com'),
72
+ ('Iris Taylor', 'Marketing', 81000, '2021-08-25', 'iris@company.com'),
73
+ ('Jack Anderson', 'Sales', 88000, '2020-12-07', 'jack@company.com'),
74
+ ('Karen Thomas', 'Engineering', 125000, '2018-05-20', 'karen@company.com'),
75
+ ('Leo Jackson', 'Finance', 98000, '2016-10-11', 'leo@company.com');
76
+
77
+ INSERT INTO projects (title, department, start_date, end_date, status, budget) VALUES
78
+ ('AI Platform v2', 'Engineering', '2024-01-01', '2024-12-31', 'active', 500000),
79
+ ('Customer Portal', 'Engineering', '2023-06-01', '2024-03-31', 'completed', 200000),
80
+ ('Q4 Campaign', 'Marketing', '2024-10-01', '2024-12-31', 'active', 150000),
81
+ ('Sales CRM Migration', 'Sales', '2024-03-01', NULL, 'active', 80000),
82
+ ('Annual Audit', 'Finance', '2024-11-01', '2024-11-30', 'completed', 30000),
83
+ ('Talent Pipeline', 'HR', '2024-07-01', NULL, 'active', 50000);
84
+
85
+ INSERT INTO sales (employee_id, product, amount, sale_date, region) VALUES
86
+ (3, 'Enterprise License', 45000, '2024-01-15', 'East'),
87
+ (4, 'SaaS Subscription', 12000, '2024-02-20', 'West'),
88
+ (10, 'Consulting Package', 28000, '2024-03-10', 'Central'),
89
+ (3, 'Enterprise License', 52000, '2024-04-05', 'East'),
90
+ (4, 'Support Plan', 8500, '2024-05-18', 'West'),
91
+ (10, 'Enterprise License', 61000, '2024-06-22', 'East'),
92
+ (3, 'SaaS Subscription', 15000, '2024-07-30', 'Central'),
93
+ (4, 'Consulting Package', 33000, '2024-08-14', 'West'),
94
+ (10, 'Support Plan', 9000, '2024-09-01', 'East'),
95
+ (3, 'Enterprise License', 47000, '2024-10-17', 'Central');
96
+ """)
97
+
98
+ conn.commit()
99
+ conn.close()
100
+ print("✅ Database initialized successfully.")
101
+
102
+
103
+ def get_schema() -> str:
104
+ """Return a CREATE TABLE schema string for the prompt."""
105
+ conn = sqlite3.connect(DB_PATH)
106
+ cursor = conn.cursor()
107
+ cursor.execute("SELECT sql FROM sqlite_master WHERE type='table' AND sql IS NOT NULL")
108
+ tables = cursor.fetchall()
109
+ conn.close()
110
+ return "\n\n".join(t[0] for t in tables)
111
+
112
+
113
+ def execute_query(sql: str):
114
+ """Execute a SQL query and return (columns, rows) or raise on error."""
115
+ conn = sqlite3.connect(DB_PATH)
116
+ cursor = conn.cursor()
117
+ try:
118
+ cursor.execute(sql)
119
+ columns = [desc[0] for desc in cursor.description] if cursor.description else []
120
+ rows = cursor.fetchall()
121
+ conn.commit()
122
+ return columns, rows
123
+ finally:
124
+ conn.close()
125
+
126
+
127
+ if __name__ == "__main__":
128
+ init_database()
129
+ schema = get_schema()
130
+ print("\n--- SCHEMA ---")
131
+ print(schema)