-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy path05-todo-sqlite.py
More file actions
69 lines (51 loc) · 1.54 KB
/
05-todo-sqlite.py
File metadata and controls
69 lines (51 loc) · 1.54 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
import sqlite3
connection = sqlite3.connect("todo.db")
def create_table(connection):
try:
cur = connection.cursor()
cur.execute("""CREATE TABLE task(task text)""")
except:
pass
def show_tasks(connection):
cur = connection.cursor()
cur.execute("""SELECT rowid, task FROM task""")
result = cur.fetchall()
for row in result:
print(str(row[0]) + " - " + row[1])
def add_task(connection):
print("dodajemy zadanie")
task = input("Wpisz treść zadania: ")
if task == "0":
print("Powrót do menu")
else:
cur = connection.cursor()
cur.execute("""INSERT INTO task(task) VALUES(?)""", (task,))
connection.commit()
print("Dodano zadanie!")
def delete_task(connection):
task_index = int(input("Podaj indeks zadania do usunięcia: "))
cur = connection.cursor()
rows_deleted = cur.execute("""DELETE FROM task WHERE rowid=?""", (task_index,)).rowcount
connection.commit()
if rows_deleted == 0:
print("Takie zadanie nie istnieje!")
else:
print("Usunięto zadanie!")
create_table(connection)
while True:
print()
print("1. Pokaż zadania")
print("2. Dodaj zadanie")
print("3. Usuń zadanie")
print("4. Wyjdź")
user_choice = int(input("Wybierz liczbę: "))
print()
if user_choice == 1:
show_tasks(connection)
if user_choice == 2:
add_task(connection)
if user_choice == 3:
delete_task(connection)
if user_choice == 4:
break
connection.close()