-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb_queries.py
More file actions
95 lines (80 loc) · 2.67 KB
/
Copy pathdb_queries.py
File metadata and controls
95 lines (80 loc) · 2.67 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
import mysql.connector as connector
def connect_to_database():
"""Establish connection to the Little Lemon database"""
try:
# Task 1: Create connection
connection = connector.connect(
user="your_username",
password="your_password",
db="LittleLemonDB"
)
print("Connected to database successfully!")
# Create cursor
cursor = connection.cursor()
return connection, cursor
except connector.Error as e:
print(f"Error connecting to database: {e}")
return None, None
def show_tables(cursor):
"""Execute query to show all tables"""
try:
# Task 2: Show all tables
show_tables_query = "SHOW tables"
cursor.execute(show_tables_query)
# Fetch all results
results = cursor.fetchall()
print("\nDatabase Tables:")
for table in results:
print(table[0])
return results
except connector.Error as e:
print(f"Error showing tables: {e}")
return None
def get_high_value_customers(cursor):
"""Get customers with orders over $60"""
try:
# Task 3: Join query for high-value customers
high_value_query = """
SELECT
CONCAT(c.FirstName, ' ', c.LastName) AS FullName,
c.Email,
c.PhoneNumber,
o.TotalAmount AS BillAmount
FROM Customers c
JOIN Orders o ON c.CustomerID = o.CustomerID
WHERE o.TotalAmount > 60
ORDER BY o.TotalAmount DESC
"""
cursor.execute(high_value_query)
results = cursor.fetchall()
print("\nHigh-Value Customers (Orders > $60):")
print("=====================================")
for customer in results:
print(f"""
Name: {customer[0]}
Email: {customer[1]}
Phone: {customer[2]}
Bill Amount: ${customer[3]:.2f}
-----------------------------------""")
return results
except connector.Error as e:
print(f"Error retrieving high-value customers: {e}")
return None
def main():
# Establish connection
connection, cursor = connect_to_database()
if connection and cursor:
try:
# Show all tables
show_tables(cursor)
# Get high-value customers
get_high_value_customers(cursor)
except connector.Error as e:
print(f"Error executing queries: {e}")
finally:
# Close cursor and connection
cursor.close()
connection.close()
print("\nDatabase connection closed.")
if __name__ == "__main__":
main()