I'm trying to make an expense tracker with Python and SQLite3. I've figured out how to input data and the fields are as follows: Amount Category(Food, Leisure, Transport, Education) Description DateOfPurchase I'm trying to create a method so I can get the sum of all the expenses of a certain category in the month and put it into a python variable (because I want to further process and create a pie chart of the different categories)
I've tried working with the SELECT SQL function but I'm not sure how to go about solving the problem
import sqlite3
import datetime
import time
connectdb = sqlite3.connect("Expenses.db")
c = connectdb.cursor()
def create_table():
c.execute("CREATE TABLE IF NOT EXISTS Expenses (Amount REAL, Category
TEXT, Description TEXT, DateOfPurchase TEXT)")
# used to input a record
def datain():
amount = float(input("Please enter the amount of money paid for the
expense"))
Today = str(datetime.datetime.fromtimestamp(time.time()).strftime('%Y-%m-%d'))
category = str(input("Please pick a category from Food, Transport,
Leisure, Education"))
temp = ("Food", "Transport", "Leisure", "Education")
while not (category in temp):
category = str(input("Please pick a category from Food,
Transport, Leisure, Education"))
description = str(input("Please describe the expense in a few
words"))
c.execute("INSERT INTO Expenses (Amount,Category,Description,DateOfPurchase) VALUES (?,?,?,?)",
(amount, category, description, Today))
rerun = str(input("Would you like to add another item? (Yes/No)"))
if (rerun == "Yes"):
datain()
else:
connectdb.commit()
c.close()
connectdb.close()