SQLite 触发器问题 - “没有这样的列”

发布于 2024-12-07 11:52:30 字数 1060 浏览 1 评论 0原文

我有一个 sqlite 数据库,其中有一个名为 Achievements 的表,它存储我正在构建的一个简单测验(学习 Objective-C 的 iPhone 项目)中是否达到了某些成就。表结构为:

ID    Name           Completed  
=============================
1     FiveInARow     0  
2     TenInARow      0  
3     AllCorrect     0  
4     UnderASecond   0  
5     AllCompleted   0  

ID 为主键,Name 为 VARCHAR,Completed 为 BOOL(0 表示 false,1 表示 true)。

我试图在该表的更新语句上添加一个触发器,这样当 Completed 列设置为 1(即成就已完成 - 这是表上将发生的唯一更新)时,将进行计算以查看是否所有其他成就均已完成,如果已完成,请将 AllCompleted 成就 (ID 5) 更新为 1。

我创建的触发器是:
更新“成就”后创建触发器“checkAllAchievements”
何时(从成就中选择 SUM(已完成))=(从成就中选择 COUNT(已完成)-1)
BEGIN UPDATE Achievements SET Completed = 1 WHERE Name= 'AllCompleted';

所以我想做的是获取已完成行的总和并将其与总计数减 1 进行比较,如果它们匹配则意味着除了 AllCompleted 成就之外的所有成就都已实现,因此我们也可以将其设置为 true。触发器创建得很好。

现在解决问题 - 当我尝试使用以下语句更新表

UPDATE Achievements SET Completed = 1 WHERE ID = 1

时,我收到错误消息“No such Column Completed”。

我是否试图做一些根本上错误的事情?难道我不能使用触发器来实现这一点吗?有什么建议吗?

谢谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

海拔太高太耀眼 2024-12-14 11:52:30

仔细检查所有内容的拼写是否与数据库中的完全一致。在您的示例中,您声明表名称为“Achievements”,但在两个位置引用“Achievement”。

解决这个问题应该可以解决您的问题。最终SQL如下:

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';
END;

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:

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';
END;
帅气称霸 2024-12-14 11:52:30

通常,这将是您正在更新的表中的突变...

触发器应该只设置值,例如

new_rec.completed := 1;

不尝试进行另一次更新。
(请原谅我的 Oracle 语法)

typically this would be a mutation in the table you are updating...

the trigger should just set the value something like

new_rec.completed := 1;

not try to do another update.
(excuse my Oracle syntax)

晨曦÷微暖 2024-12-14 11:52:30

我认为您不应该在更新该表时从触发器更新该表,这可能是无限递归。也许 SQLite 不喜欢这样,但给出了一个错误的诊断

I think you should not update a table from a trigger on updating of that table, could be a infinite recursion. Maybe SQLite doesn't like that, but give a bad diagnositic

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文