I am working with an SQLite database. Is this code written the best way it can be and correct? The code:
Selects from the db adding a column for the percentage difference between two columns
Loops through the values of the columns
If the date is in the past
If the price is greater than 500 and the percentage difference is less than 1st argument set flag to 1
Else if the price is less than 500 and the percentage difference is less than 2nd argument set flag to 1
Otherwise keep the flag as 0
from datetime import datetime as dt
def calculateEmployeeSpend(read_cursor, flag_higher_amount, flag_lower_budget):
read_cursor.execute("SELECT distinct b.employee_id, b.amount, "
"s.spend, b.date, b.amount - s.spend as spend_left, "
"100.0*(b.amount - s.spend) / b.amount As PercentDiff FROM employee_budget_upload "
"As b JOIN employee_budget_spent As s ON b.employee_id = s.employee_id where b.amount != 0")
for employee_id, amount, spend, date, spend_left, percent_diff in read_cursor:
flag=0
date_of_amount = dt.strptime(date, "%d/%m/%Y")
if date_of_amount <= dt.now():
if amount > 500 and percent_diff < int(flag_higher_amount) or amount < 500 and percent_diff < int(flag_lower_budget):
flag=1
write_cursor.execute('INSERT OR REPLACE INTO employee_overall_spend VALUES (?,?,?,?,?,?,?,?)',
(employee_id, amount, spend, date, spend_left, percent_diff, dt.now().strftime("%d/%m/%Y"), flag))
flag
variable. The code that you've posted doesn't use it... Please add more code so we can actually comment on what your code is doing. \$\endgroup\$write_cursor
is indented. Please post your actual code. \$\endgroup\$