|
| 1 | +import sqlite3 |
| 2 | + |
| 3 | +# Connect to the database |
| 4 | +conn = sqlite3.connect('expenses.db') |
| 5 | +cursor = conn.cursor() |
| 6 | + |
| 7 | +# Define SQL statements |
| 8 | +create_table_sql = """CREATE TABLE IF NOT EXISTS expenses (id INTEGER PRIMARY KEY AUTOINCREMENT, date TEXT, description TEXT, amount REAL)""" |
| 9 | +insert_expense_sql = "INSERT INTO expenses (date, description, amount) VALUES (?, ?, ?)" |
| 10 | +select_expenses_sql = "SELECT * FROM expenses ORDER BY date;" |
| 11 | +delete_expense_by_id_sql = "DELETE FROM expenses WHERE id=?" |
| 12 | +update_expense_by_id_sql = "UPDATE expenses SET description=? WHERE id=?" |
| 13 | + |
| 14 | +# Check if the database exists, create it if not |
| 15 | +if not conn.execute("SELECT name FROM sqlite_master WHERE type='table' AND name='expenses';").fetchone(): |
| 16 | + cursor.execute(create_table_sql) |
| 17 | + conn.commit() |
| 18 | + |
| 19 | +def add_expense(): |
| 20 | + global cursor |
| 21 | + |
| 22 | + # Get input from user |
| 23 | + print("Enter date (YYYY-MM-DD): ") |
| 24 | + date = input().split()[0] + '-01' |
| 25 | + print("Enter description: ") |
| 26 | + description = input() |
| 27 | + print("Enter amount: ") |
| 28 | + amount = float(input()) |
| 29 | + |
| 30 | + try: |
| 31 | + cursor.execute(insert_expense_sql, (date, description, amount)) |
| 32 | + conn.commit() |
| 33 | + print("Expense added successfully.") |
| 34 | + except Exception as e: |
| 35 | + print("Error adding expense: {}".format(e)) |
| 36 | + |
| 37 | +def delete_expense(): |
| 38 | + global cursor |
| 39 | + |
| 40 | + # Get ID from user |
| 41 | + print("Enter ID to delete: ") |
| 42 | + id = int(input()) |
| 43 | + |
| 44 | + try: |
| 45 | + cursor.execute(delete_expense_by_id_sql, (id,)) |
| 46 | + conn.commit() |
| 47 | + print("Expense deleted successfully.") |
| 48 | + except Exception as e: |
| 49 | + print("Error deleting expense: {}".format(e)) |
| 50 | + |
| 51 | +def update_expense(): |
| 52 | + global cursor |
| 53 | + |
| 54 | + # Get ID and new description from user |
| 55 | + print("Enter ID to update: ") |
| 56 | + id = int(input()) |
| 57 | + print("Enter new description: ") |
| 58 | + description = input() |
| 59 | + |
| 60 | + try: |
| 61 | + cursor.execute(update_expense_by_id_sql, (description, id)) |
| 62 | + conn.commit() |
| 63 | + print("Expense updated successfully.") |
| 64 | + except Exception as e: |
| 65 | + print("Error updating expense: {}".format(e)) |
| 66 | + |
| 67 | + |
| 68 | +def view_expenses(): |
| 69 | + # Fetch and display expenses |
| 70 | + expenses = conn.execute(select_expenses_sql).fetchall() |
| 71 | + if not expenses: |
| 72 | + print("No expenses recorded yet.") |
| 73 | + else: |
| 74 | + print("Expenses:") |
| 75 | + for expense in expenses: |
| 76 | + print(f"ID: {expense[0]}, Date: {expense[1]}, Description: {expense[2]}, Amount: Rs: {expense[3]}") |
| 77 | + |
| 78 | +def total_expenses(): |
| 79 | + # Calculate and display total expenses |
| 80 | + total = conn.execute("SELECT SUM(amount) FROM expenses;").fetchone()[0] |
| 81 | + print(f"Total expenses: {total}$") |
| 82 | + |
| 83 | +def main_menu(): |
| 84 | + while True: |
| 85 | + print("\nExpense Tracker Menu:") |
| 86 | + print("1. Add Expense") |
| 87 | + print("2. View Expenses") |
| 88 | + print("3. Total Expenses") |
| 89 | + print("4. Delete Expense") |
| 90 | + print("5. Update Expense Description") |
| 91 | + print("6. Quit") |
| 92 | + |
| 93 | + choice = input("Enter your choice (1-6): ") |
| 94 | + |
| 95 | + if choice == '1': |
| 96 | + add_expense() |
| 97 | + elif choice == '2': |
| 98 | + view_expenses() |
| 99 | + elif choice == '3': |
| 100 | + total_expenses() |
| 101 | + elif choice == '4': |
| 102 | + delete_expense() |
| 103 | + elif choice == '5': |
| 104 | + update_expense() |
| 105 | + elif choice == '6': |
| 106 | + break |
| 107 | + else: |
| 108 | + print("Invalid choice. Please try again.") |
| 109 | + |
| 110 | + |
| 111 | +if __name__ == "__main__": |
| 112 | + main_menu() |
| 113 | + |
| 114 | +conn.close() |
0 commit comments