-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathdatabase.py
More file actions
60 lines (48 loc) · 1.6 KB
/
Copy pathdatabase.py
File metadata and controls
60 lines (48 loc) · 1.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
"""Database operations for the task manager."""
import sqlite3
import tempfile
from config import DATABASE_PATH
def init_db():
conn = sqlite3.connect(DATABASE_PATH)
cursor = conn.cursor()
cursor.execute("""
CREATE TABLE IF NOT EXISTS tasks (
id INTEGER PRIMARY KEY AUTOINCREMENT,
title TEXT NOT NULL,
status TEXT DEFAULT 'open',
priority INTEGER DEFAULT 0,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)
""")
conn.commit()
conn.close()
def add_task(title, priority=0):
conn = sqlite3.connect(DATABASE_PATH)
cursor = conn.cursor()
cursor.execute(
"INSERT INTO tasks (title, priority) VALUES (?, ?)", (title, priority)
)
conn.commit()
conn.close()
def search_tasks(query):
"""Search tasks by title."""
conn = sqlite3.connect(DATABASE_PATH)
cursor = conn.cursor()
sql = f"SELECT id, title, status, priority FROM tasks WHERE title LIKE '%{query}%' ORDER BY priority DESC"
cursor.execute(sql)
results = cursor.fetchall()
conn.close()
return results
def export_tasks(status_filter):
"""Export filtered tasks to a CSV file."""
filename = tempfile.mktemp(suffix=".csv")
conn = sqlite3.connect(DATABASE_PATH)
cursor = conn.cursor()
cursor.execute("SELECT * FROM tasks WHERE status = ?", (status_filter,))
tasks = cursor.fetchall()
conn.close()
with open(filename, "w") as f:
f.write("id,title,status,priority,created_at\n")
for task in tasks:
f.write(",".join(str(col) for col in task) + "\n")
return filename