I'm currently working on a Python project, where I need to use a REPLACE into query. The table is for budgets, and is supposed to update the column for either expenses or incomes, depending on the value.
My idea is, to check if it's either an expense or incomes in the initial if statement. Then the query column name 'TYPE' is supposed to change based on the if statement. Atlast as usual, I will format the values into the query to execute.
How can I properly format the query based on my if-statement?
PYTHON:
for key, value in budget.items():
if value > 0:
type = "incomes"
else:
type = "expenses"
updateQuery = """
REPLACE into budget (
username,
{type},
categories
) VALUES ('{}', '{}', '{}');
""".format(username, type, key)
insertTuple = (username, value)
cursor.execute(updateQuery, insertTuple)
self.__dbConnector.commit()
M$SQL
CREATE TABLE budget (
budget_id VARCHAR(255),
incomes MONEY,
expense MONEY,
savings MONEY,
investments MONEY,
categories VARCHAR(255),
FOREIGN KEY(budget_id) REFERENCES user (username)
);