-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.py
More file actions
130 lines (105 loc) · 3.37 KB
/
Copy pathdatabase.py
File metadata and controls
130 lines (105 loc) · 3.37 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
# Made with ❤️ by Garry
# 27/01/24
class database:
def __init__(
self,
): # Import necessary modules, then create and initialise a database with a table if one doesn't already exist
import sqlite3, datetime, os
DB_FILE = "data.db"
self.date = lambda: datetime.datetime.now().strftime("%I:%M %p %a %d %b %Y")
init_db = False
if not os.path.exists(DB_FILE):
init_db = True
self.db = sqlite3.connect(DB_FILE)
self.cursor = self.db.cursor()
if init_db:
self.cursor.execute(
"""
CREATE TABLE tasks (
task_id INTEGER PRIMARY KEY AUTOINCREMENT,
task_title TEXT NOT NULL UNIQUE,
task_desc TEXT,
task_created REAL
);
"""
)
self.db.commit()
def task_list(self): # Return a list of tasks
tasks = self.cursor.execute(
"SELECT task_title, task_desc, task_created FROM tasks;"
).fetchall()
results = [task for task in tasks]
return results
def task_add(self, title, desc): # Add a task to the database
if not desc:
desc = "N/A"
created = self.date()
self.cursor.execute(
f"""
INSERT INTO tasks (task_title, task_desc, task_created)
VALUES (\"{title}\",\"{desc}\",\"{created}\");"""
)
self.db.commit()
def task_del(self, id): # Delete a task by its task_id
try:
self.cursor.execute(
f"""
DELETE FROM tasks WHERE task_id = \"{id}\";
"""
)
self.db.commit()
except Exception as e:
print("Error deleting task:", e)
def task_edit(
self, id, title, desc
): # Change values of a task to the ones provided when calling the function
try:
if title:
self.cursor.execute(
f"""
UPDATE tasks
SET task_title = \"{title}\"
WHERE task_id = \"{id}\";
"""
)
if desc:
self.cursor.execute(
f"""
UPDATE tasks
SET task_desc = \"{desc}\"
WHERE task_id = \"{id}\";
"""
)
self.db.commit()
except Exception as e:
print("Error editing task:", e)
def lookup_by_title(
self, title
): # Return a task_id for a task with a certain task_title
id = self.cursor.execute(
f"""
SELECT task_id FROM tasks
WHERE task_title = \"{title}\"
"""
).fetchone()[0]
return id
def lookup_by_id(self, id): # Unused function
tasks = self.cursor.execute(
f"""
SELECT task_id, task_title, task_desc, task_created FROM tasks
WHERE task_id = \"{id}\"
"""
).fetchall()
results = []
for task in tasks:
results.append(task)
return results
def drop(self): # Clear the database
self.cursor.execute("DELETE FROM tasks;")
self.db.commit()
def end(self): # Safely close the database
self.db.commit()
self.db.close()
if __name__ == "__main__": # nuh uh
print("nuh uh")
exit()