-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevent_manager.py
More file actions
45 lines (40 loc) · 1.21 KB
/
event_manager.py
File metadata and controls
45 lines (40 loc) · 1.21 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
import sqlite3
from pathlib import Path
# Database setup (runs once when the file is imported)
DB_PATH = Path("events.db")
def init_db():
conn = sqlite3.connect(DB_PATH)
cursor = conn.cursor()
cursor.execute("""
CREATE TABLE IF NOT EXISTS events (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
date TEXT NOT NULL,
time TEXT,
location TEXT,
user_id INTEGER NOT NULL
)
""")
conn.commit()
conn.close()
# Initialize database on import
init_db()
def add_event(name, date, time=None, location=None, user_id=None):
"""Add a new event to the local database."""
conn = sqlite3.connect(DB_PATH)
cursor = conn.cursor()
cursor.execute("""
INSERT INTO events (name, date, time, location, user_id)
VALUES (?, ?, ?, ?, ?)
""", (name, date, time, location, user_id))
conn.commit()
conn.close()
print(f"✅ Event '{name}' on {date} added successfully.")
def get_all_events():
"""Retrieve all events from the database."""
conn = sqlite3.connect(DB_PATH)
cursor = conn.cursor()
cursor.execute("SELECT * FROM events ORDER BY date")
events = cursor.fetchall()
conn.close()
return events