I have the following function is python, which is not working:
def get_expenses_by_type(self, current_user):
try:
cursor = self.main_db.cursor(dictionary=True)
if self.check_user_level(current_user) == 0:
query = """
SELECT Date, Type, SUM(Price * Quantity) AS TotalAmount
FROM ExpenseTable
WHERE UserID = %s
GROUP BY Date, Type;
"""
else:
query = """
SELECT Date, Type, SUM(Price * Quantity) AS TotalAmount
FROM ExpenseTable
GROUP BY Date, Type;
"""
cursor.execute(query, (current_user,))
results = cursor.fetchall()
cursor.close()
return results
except Exception as error:
raise error
When I run the code, if the user level is greater than 0, I get the following error raised:
mysql.connector.errors.ProgrammingError: Not all parameters were used in the SQL statement
Could anyone help please?
2
Answers
I realised the solution was within how I was executing the query. In either scenario, I was executing the query and using the parameter of the current user:
cursor.execute(query, (current_user,))
. The solution was to have two execute statements, one in each selection:This worked as intended.
You can move the query parameters into another variable. Then you can make this an empty tuple when you don’t have a
WHERE
clause.