-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase.py
43 lines (37 loc) · 1.16 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
import sqlite3
DB_NAME = 'tasks.db'
def init_db():
conn = sqlite3.connect(DB_NAME)
c = conn.cursor()
c.execute('''CREATE TABLE IF NOT EXISTS tasks (
id INTEGER PRIMARY KEY AUTOINCREMENT,
description TEXT NOT NULL,
done BOOLEAN NOT NULL
)''')
conn.commit()
conn.close()
def get_tasks():
conn = sqlite3.connect(DB_NAME)
c = conn.cursor()
c.execute("SELECT * FROM tasks")
tasks = [{'id': row[0], 'description': row[1], 'done': row[2]} for row in c.fetchall()]
conn.close()
return tasks
def add_task(description):
conn = sqlite3.connect(DB_NAME)
c = conn.cursor()
c.execute("INSERT INTO tasks (description, done) VALUES (?, ?)", (description, False))
conn.commit()
conn.close()
def delete_task(task_id):
conn = sqlite3.connect(DB_NAME)
c = conn.cursor()
c.execute("DELETE FROM tasks WHERE id=?", (task_id,))
conn.commit()
conn.close()
def mark_task_done(task_id):
conn = sqlite3.connect(DB_NAME)
c = conn.cursor()
c.execute("UPDATE tasks SET done=? WHERE id=?", (True, task_id))
conn.commit()
conn.close()