-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquery.py
More file actions
139 lines (110 loc) · 3.38 KB
/
query.py
File metadata and controls
139 lines (110 loc) · 3.38 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
"""
Simple Database Query Script
Modify the query variable below to run any SQL query
"""
import sqlite3
from pathlib import Path
# Get the directory where this script is located
SCRIPT_DIR = Path(__file__).parent
DB_PATH = SCRIPT_DIR / "agora.db"
# Connect to database
conn = sqlite3.connect(DB_PATH)
cursor = conn.cursor()
# ============================================
# WRITE YOUR QUERY HERE
# ============================================
query = """
INSERT INTO user (email, name, university, reputation_score, created_at)
VALUES ('test@gmail.com', 'Test User', 'Saint Cloud State University', 0, datetime('now'))
"""
# ============================================
# Execute and display results
# ============================================
try:
cursor.execute(query)
# Check if this is a SELECT query (returns data)
if query.strip().upper().startswith('SELECT') or query.strip().upper().startswith('PRAGMA'):
rows = cursor.fetchall()
# Get column names
column_names = [description[0] for description in cursor.description]
print("\n" + "="*80)
print(f"QUERY: {query.strip()}")
print("="*80)
print(f"\nFound {len(rows)} rows\n")
# Print column headers
print(" | ".join(column_names))
print("-" * 80)
# Print rows
for row in rows:
print(" | ".join(str(value) for value in row))
print("\n")
else:
# This is an INSERT, UPDATE, DELETE, or other modifying query
conn.commit()
print("\n" + "="*80)
print(f"QUERY: {query.strip()}")
print("="*80)
print(f"\n✓ Success! {cursor.rowcount} row(s) affected\n")
except sqlite3.Error as e:
print(f"\n✗ Error: {e}\n")
conn.rollback()
finally:
conn.close()
# ============================================
# EXAMPLE QUERIES (copy & paste above)
# ============================================
# Get all users
# query = "SELECT * FROM user"
# Get all courses
# query = "SELECT * FROM course"
# INSERT a new user (with all fields to avoid None values)
# query = """
# INSERT INTO user (email, name, university, reputation_score, created_at)
# VALUES ('alice.brown@university.edu', 'Alice Brown', 'University of Example', 0, datetime('now'))
# """
# INSERT a new course
# query = """
# INSERT INTO course (code, name, description)
# VALUES ('ENGL 101', 'English Composition', 'Introduction to academic writing')
# """
# UPDATE user reputation
# query = """
# UPDATE user
# SET reputation_score = 100
# WHERE email = 'john.doe@university.edu'
# """
# DELETE a user (be careful!)
# query = """
# DELETE FROM user
# WHERE email = 'test@university.edu'
# """
# Get enrollments with user names
# query = """
# SELECT u.name, c.code, c.name
# FROM enrollment e
# JOIN user u ON e.user_id = u.user_id
# JOIN course c ON e.course_id = c.course_id
# """
# Count users per university
# query = """
# SELECT university, COUNT(*) as user_count
# FROM user
# GROUP BY university
# """
# Get user with highest reputation
# query = """
# SELECT name, email, reputation_score
# FROM user
# ORDER BY reputation_score DESC
# LIMIT 5
# """
# List all tables in database
# query = """
# SELECT name FROM sqlite_master
# WHERE type='table'
# ORDER BY name
# """
# Get table structure
# query = "PRAGMA table_info(user)"
# Get all tags
# query = "SELECT * FROM tag"