-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.py
More file actions
99 lines (81 loc) · 2.5 KB
/
database.py
File metadata and controls
99 lines (81 loc) · 2.5 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
import sqlite3
database_name = "task.db"
conn = sqlite3.connect("task.db")
cursor = conn.cursor()
cursor.execute("""
CREATE TABLE IF NOT EXISTS task(
id INTEGER PRIMARY KEY AUTOINCREMENT,
description TEXT NOT NULL, status TEXT DEFAULT 'Pending')""")
conn.commit()
conn.close
def add(description):
try:
conn = sqlite3.connect("task.db")
cursor = conn.cursor()
cursor.execute("""INSERT INTO task(description)
VALUES(?) """, (description,))
conn.commit()
print("task added sucessfully")
except sqlite3.Error as e:
print(f"Database error: {e}")
finally:
conn.close()
def list():
try:
conn = sqlite3.connect("task.db")
cursor = conn.cursor()
cursor.execute("""SELECT * FROM task""")
rows= cursor.fetchall()
return rows
except sqlite3.Error as e:
print(f"Database error: {e}")
finally:
conn.close()
def done(id):
try:
conn = sqlite3.connect("task.db")
cursor = conn.cursor()
cursor.execute("""UPDATE task SET status='Done'
WHERE id=? """,(id,))
conn.commit()
print("task completed")
except sqlite3.Error as e:
print(f"Database error: {e}")
finally:
conn.close()
def delete(id):
try:
conn = sqlite3.connect("task.db")
cursor = conn.cursor()
cursor.execute("""DELETE FROM task WHERE id=?""", (id,))
conn.commit()
print("task deleted")
except sqlite3.Error as e:
print(f"Database error: {e}")
finally:
conn.close()
def edit(id, edited_description):
try:
conn = sqlite3.connect("task.db")
cursor = conn.cursor()
cursor.execute("""UPDATE task SET description=? WHERE id=?""",
(edited_description, id))
conn.commit()
print("task updated")
except sqlite3.Error as e:
print(f"Database error: {e}")
finally:
conn.close()
def reset_table():
try:
conn = sqlite3.connect("task.db")
cursor = conn.cursor()
cursor.execute("DELETE FROM task;")
cursor.execute("""DELETE FROM sqlite_sequence
WHERE name='task'""")
conn.commit()
print("database reset successfully")
except sqlite3.Error as e:
print(f"Database error: {e}")
finally:
conn.close()