-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
203 lines (166 loc) · 5.93 KB
/
app.py
File metadata and controls
203 lines (166 loc) · 5.93 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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
"""
Agora Database Application
Connects to MySQL and demonstrates SELECT, INSERT, UPDATE, DELETE
using prepared statements. Also executes one transaction programmatically.
Requirements:
pip install mysql-connector-python
Usage:
python app.py
Set the DB_* constants below to match your MySQL connection.
"""
import mysql.connector
from mysql.connector import Error
DB_HOST = "localhost"
DB_PORT = 3306
DB_NAME = "agora_db"
DB_USER = "root"
DB_PASS = ""
def connect():
return mysql.connector.connect(
host=DB_HOST,
port=DB_PORT,
database=DB_NAME,
user=DB_USER,
password=DB_PASS
)
def print_rows(cursor, label):
rows = cursor.fetchall()
cols = [d[0] for d in cursor.description]
print(f"\n--- {label} ---")
print(" " + " | ".join(cols))
print(" " + "-" * 60)
for row in rows:
print(" " + " | ".join(str(v) for v in row))
print(f" ({len(rows)} row(s))")
# ------------------------------------------------------------------
# SELECT: top 5 users by reputation score
# ------------------------------------------------------------------
def select_top_users(conn):
cursor = conn.cursor()
sql = """
SELECT user_id, name, reputation_score
FROM User
ORDER BY reputation_score DESC
LIMIT %s
"""
cursor.execute(sql, (5,))
print_rows(cursor, "Top 5 Users by Reputation")
cursor.close()
# ------------------------------------------------------------------
# SELECT: all problems for a given course with answer counts
# ------------------------------------------------------------------
def select_problems_for_course(conn, course_id):
cursor = conn.cursor()
sql = """
SELECT pr.problem_id,
pr.title,
COUNT(a.answer_id) AS answer_count,
MAX(a.is_accepted) AS has_accepted
FROM Problem pr
LEFT JOIN Answer a ON pr.problem_id = a.problem_id
WHERE pr.course_id = %s
GROUP BY pr.problem_id, pr.title
ORDER BY pr.created_at DESC
"""
cursor.execute(sql, (course_id,))
print_rows(cursor, f"Problems for course_id={course_id}")
cursor.close()
# ------------------------------------------------------------------
# INSERT: add a new user
# ------------------------------------------------------------------
def insert_user(conn, email, name, university):
cursor = conn.cursor()
sql = """
INSERT INTO User (email, name, university, reputation_score)
VALUES (%s, %s, %s, %s)
"""
cursor.execute(sql, (email, name, university, 0))
conn.commit()
new_id = cursor.lastrowid
print(f"\n--- INSERT User ---")
print(f" Inserted user_id={new_id}: {name} ({email})")
cursor.close()
return new_id
# ------------------------------------------------------------------
# UPDATE: increment a user's reputation score
# ------------------------------------------------------------------
def update_reputation(conn, user_id, points):
cursor = conn.cursor()
sql = """
UPDATE User
SET reputation_score = reputation_score + %s
WHERE user_id = %s
"""
cursor.execute(sql, (points, user_id))
conn.commit()
print(f"\n--- UPDATE Reputation ---")
print(f" Added {points} points to user_id={user_id} ({cursor.rowcount} row affected)")
cursor.close()
# ------------------------------------------------------------------
# DELETE: remove a user by user_id
# ------------------------------------------------------------------
def delete_user(conn, user_id):
cursor = conn.cursor()
sql = "DELETE FROM User WHERE user_id = %s"
cursor.execute(sql, (user_id,))
conn.commit()
print(f"\n--- DELETE User ---")
print(f" Deleted user_id={user_id} ({cursor.rowcount} row affected)")
cursor.close()
# ------------------------------------------------------------------
# TRANSACTION: enroll a user in a course atomically
# Inserts the enrollment and logs a summary; rolls back on error.
# ------------------------------------------------------------------
def enroll_user(conn, user_id, course_id):
cursor = conn.cursor()
try:
conn.start_transaction()
# Check the course exists
cursor.execute(
"SELECT name FROM Course WHERE course_id = %s",
(course_id,)
)
course = cursor.fetchone()
if not course:
raise ValueError(f"course_id={course_id} does not exist")
# Insert enrollment
cursor.execute(
"INSERT INTO Enrollment (user_id, course_id) VALUES (%s, %s)",
(user_id, course_id)
)
conn.commit()
print(f"\n--- TRANSACTION: Enroll User ---")
print(f" user_id={user_id} enrolled in '{course[0]}' (course_id={course_id})")
except Error as e:
conn.rollback()
print(f"\n--- TRANSACTION ROLLED BACK ---")
print(f" Reason: {e}")
finally:
cursor.close()
# ------------------------------------------------------------------
# Main
# ------------------------------------------------------------------
def main():
try:
conn = connect()
print(f"Connected to {DB_NAME} on {DB_HOST}")
select_top_users(conn)
select_problems_for_course(conn, course_id=2)
new_user_id = insert_user(
conn,
email="demo.student@stcloudstate.edu",
name="Demo Student",
university="Saint Cloud State University"
)
update_reputation(conn, user_id=new_user_id, points=25)
# Enroll the new user in Database Systems (course_id = 2)
enroll_user(conn, user_id=new_user_id, course_id=2)
# Attempt a duplicate enrollment to show rollback behavior
enroll_user(conn, user_id=new_user_id, course_id=2)
delete_user(conn, user_id=new_user_id)
conn.close()
print("\nDone.")
except Error as e:
print(f"Connection error: {e}")
if __name__ == "__main__":
main()