-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtodolist _with _dtabase.py
More file actions
45 lines (34 loc) · 1.25 KB
/
todolist _with _dtabase.py
File metadata and controls
45 lines (34 loc) · 1.25 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
#library that helps us use a database in ptython
import sqlite3
#connect to or create a database file
conn = sqlite3.connect("todo.db")
#helps send commands to the database
cursor = conn.cursor()
#create a table to store tasks
cursor.execute
def show_tasks():
cursor.execute("SELECT * FROM tasks") # show me everything in the task table
rows = cursor.fetchall() # get the results as a list
print('Your to do list: ')
for row in rows :
print(f"{row[0]}. {row[1]}")
def add_tasks(task):
cursor.execute("INSERT INTO tasks (tasks) VALUES (?)", (task,)) # Add the task.
conn.commit() # Save changes.
def delete_tasks(task_id):
cursor.execute("DELETE FROM tasks WHERE id = ?", (task_id,)) # Remove the task.
conn.commit() # Save changes.
while True: # Keeps the program running until you type "quit."
action = input("\nWhat do you want to do? (add, view, delete, quit): ").lower()
if action == "add":
task = input("Enter a new task: ")
add_tasks(task)
elif action == "view":
show_tasks()
elif action == "delete":
task_id = int(input("Enter the task ID to delete: "))
delete_tasks(task_id)
elif action == "quit":
break
else:
print("Invalid option!")