-
Notifications
You must be signed in to change notification settings - Fork 0
/
database.py
55 lines (47 loc) · 1.59 KB
/
database.py
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
import sqlite3
class TaskDatabase:
def __init__(self, db_file):
self.connection = sqlite3.connect(db_file)
self.create_tables()
def create_tables(self):
cursor = self.connection.cursor()
cursor.execute('''
CREATE TABLE IF NOT EXISTS tasks (
id INTEGER PRIMARY KEY,
description TEXT NOT NULL,
completed INTEGER DEFAULT 0
)
''')
self.connection.commit()
def insert_task(self, description):
cursor = self.connection.cursor()
cursor.execute('''
INSERT INTO tasks (description) VALUES (?)
''', (description,))
self.connection.commit()
def get_all_tasks(self):
cursor = self.connection.cursor()
cursor.execute('''
SELECT id, description FROM tasks WHERE completed = 0
''')
return cursor.fetchall()
def get_completed_tasks(self):
cursor = self.connection.cursor()
cursor.execute('''
SELECT id, description FROM tasks WHERE completed = 1
''')
return cursor.fetchall()
def mark_task_complete(self, task_id):
cursor = self.connection.cursor()
cursor.execute('''
UPDATE tasks SET completed = 1 WHERE id = ?
''', (task_id,))
self.connection.commit()
def delete_task(self, task_id):
cursor = self.connection.cursor()
cursor.execute('''
DELETE FROM tasks WHERE id = ?
''', (task_id,))
self.connection.commit()
def close(self):
self.connection.close()