-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.py
More file actions
144 lines (108 loc) · 3.6 KB
/
Copy pathdatabase.py
File metadata and controls
144 lines (108 loc) · 3.6 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
135
import sqlite3
import os
def create_sample_database():
"""
This function build a sample database.
Tables: employees, sales, and products.
"""
#Create database file
#If database exists, just open it
db_path = "company.db"
connection = sqlite3.connect(db_path)
#cursor is like a workspace
cursor = connection.cursor()
# --- Create the employees table ---
cursor.execute("""
CREATE TABLE IF NOT EXISTS employees (
id INTEGER PRIMARY KEY,
name TEXT,
dept TEXT,
salary INTEGER
)
""")
# --- Create the products table ---
cursor.execute("""
CREATE TABLE IF NOT EXISTS products (
id INTEGER PRIMARY KEY,
name TEXT,
price REAL,
stock INTEGER
)
""")
# --- Create the sales table ---
# Note: employee_id and product_id are "foreign keys"
# They connect this table back to the other two
cursor.execute("""
CREATE TABLE IF NOT EXISTS sales (
id INTEGER PRIMARY KEY,
employee_id INTEGER,
product_id INTEGER,
quantity INTEGER,
sale_date TEXT
)
""")
# --- Fill in some sample rows ---
cursor.executemany("INSERT OR IGNORE INTO employees VALUES (?,?,?,?)", [
(1, "Alice", "Engineering", 95000),
(2, "Bob", "Sales", 72000),
(3, "Carol", "Sales", 68000),
(4, "David", "Engineering", 88000),
(5, "Eve", "Marketing", 74000),
])
cursor.executemany("INSERT OR IGNORE INTO products VALUES (?,?,?,?)", [
(1, "Laptop", 999.99, 50),
(2, "Monitor", 349.99, 120),
(3, "Keyboard", 79.99, 300),
(4, "Mouse", 49.99, 400),
])
cursor.executemany("INSERT OR IGNORE INTO sales VALUES (?,?,?,?,?)", [
(1, 2, 1, 3, "2024-01-15"),
(2, 3, 2, 5, "2024-01-18"),
(3, 2, 3, 10, "2024-02-02"),
(4, 3, 1, 2, "2024-02-14"),
(5, 2, 4, 8, "2024-03-01"),
(6, 3, 2, 3, "2024-03-10"),
(7, 2, 1, 1, "2024-03-22"),
(8, 3, 3, 6, "2024-04-05"),
])
# Save everything to disk
connection.commit()
print("Database created at: ")
return connection
def run_query(connection, sql):
"""
Runs a SQL query and prints the results
"""
cursor = connection.cursor()
try:
cursor.execute(sql)
rows = cursor.fetchall()
return {"success": True, "rows": rows}
except sqlite3.Error as e:
return {"success": False, "error": str(e)}
def print_result(result):
"""
Print a result dictionary from run_query in a readable way.
Separated from run_query so the validation loop can check
success/failure before deciding whether to display anything.
"""
if not result["success"]:
print (f"SQL Error: {result["error"]}")
return
rows = result["rows"]
#columns = result["columns"]
if not rows:
print("(query ran successfully, but returned no rows)")
return
#print(" | ".join(columns))
print("-" * 40)
for row in rows:
print(" | ".join(str(val) for val in row))
if __name__ == "__main__":
conn = create_sample_database()
print("\n--- All employees ---")
run_query(conn, "SELECT * FROM employees")
print("\n--- Products under $100 ---")
run_query(conn, "SELECT name, price FROM products WHERE price < 100")
print("\n--- My own query ---")
run_query(conn, "SELECT name, salary FROM employees WHERE dept = 'Engineering'")