-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExpenseTracker.py
More file actions
76 lines (66 loc) · 1.71 KB
/
Copy pathExpenseTracker.py
File metadata and controls
76 lines (66 loc) · 1.71 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
import sqlite3
connection = sqlite3.connect("expenses.db")
cursor = connection.cursor()
cursor.execute("""
CREATE TABLE IF NOT EXISTS expenses(
id INTEGER PRIMARY KEY AUTOINCREMENT,
amount INTEGER,
category TEXT,
date TEXT
)
""")
def insert():
amount = input("Enter the amount : ")
category = input("Enter the category : ")
date = input("Enter the date : dd-mm-yyyy \n")
cursor.execute(
"INSERT INTO expenses (amount,category,date) VALUES (?, ?, ?)", (amount,category,date)
)
connection.commit()
def display():
cursor.execute(
"SELECT * FROM expenses"
)
for row in cursor.fetchall():
print(row)
connection.commit()
def search_category():
cat = input("Enter the category : ")
cursor.execute(
"SELECT * FROM expenses WHERE category = ?", (cat, )
)
for row in cursor.fetchall():
print(row)
connection.commit()
def delete():
index = input("Enter the expense id : ")
cursor.execute(
"DELETE FROM expenses WHERE id = ?",(index, )
)
connection.commit()
def total_expenses():
total = 0
cursor.execute(
"SELECT amount FROM expenses"
)
for i in cursor.fetchall():
total += i[0]
connection.commit()
print(total)
while True:
choice = input("1- Add an expense\n2- Display all expenses\n3- Search by category\n4- Delete an expense\n5- Total expenses\n6- Exit")
match choice:
case "1":
insert()
case "2":
display()
case "3":
search_category()
case "4":
delete()
case "5":
total_expenses()
case "6":
break
case _:
print("Invalid input")