I have a sqlite database with a table named Achievements, it stores whether certain achievements have been met in a simple quiz I am building (iphone project to learn objective-c). The table structure is:
ID Name Completed
=============================
1 FiveInARow 0
2 TenInARow 0
3 AllCorrect 0
4 UnderASecond 0
5 AllCompleted 0
The ID is the primary key, Name is a VARCHAR and Completed is BOOL (0 for false and 1 for true).
I am trying to add a trigger onto the update statements of this table, such that when a Completed column is set to 1 (i.e. achievement completed – this is the only update that will happen on the table) there will be a calculation to see whether all of the other achievements have been completed and if so also update the AllCompleted achievement (ID 5) to 1.
The trigger I created is:
CREATE TRIGGER "checkAllAchievements" AFTER UPDATE ON "Achievements"
WHEN (Select SUM(Completed) from Achievements) = (Select COUNT(Completed) -1 from Achievements)
BEGIN UPDATE Achievements SET Completed = 1 WHERE Name= 'AllCompleted';
So what I am trying to do is take the sum of the completed row and compare it to the total count minus 1, if they match then it means that all achievements apart from the AllCompleted achievement are achieved so we can also set that to true. The trigger gets created fine.
Now on to the problem – When I attempt to update the table with the following statement
UPDATE Achievements SET Completed = 1 WHERE ID = 1
I receive the error message “No such Column Completed”.
Am I trying to do something that is fundamentally wrong? Is it not possible for me to achieve this using a trigger? Any advice?
Thanks.
Double check that you spelled everything exactly as it is in the database. In your example, you state the table name is “Achievements” however reference “Achievement” in two places.
Fixing that should solve your issue. Final SQL is as follows: