sqlite触发器导致“无此列”例外 [英] sqlite trigger causes "no such column" exception
问题描述
我是使用sql触发器的新手,并且在一个看似简单的示例中遇到了ESQLiteException。当我尝试修改现有行中的 memberTag列时,出现异常无此类列:memberTag。如果我放下触发器,则异常消失并且该行得到更新。
I'm a newbie with sql triggers and am getting an ESQLiteException on what seems like a simple example. When I try to modify the "memberTag" column in an existing row, I get the exception "no such column: memberTag". If I drop the trigger, the exception goes away and the row gets updated.
我正在使用SQLite,并且正在使用 SQLite Expert Personal应用程序来进行此实验。
I'm using SQLite and I'm using the "SQLite Expert Personal" app to do this experimenting.
我有此表:
CREATE TABLE [znode] (
[description] CHAR NOT NULL,
[memberTag] CHAR);
,然后触发:
CREATE TRIGGER [memberTagTrigger]
AFTER UPDATE
ON [znode]
FOR EACH ROW
WHEN length(memberTag)=0
BEGIN
update znode
set memberTag = null;
END;
我的更新实验数据如下:
My update experiment data is something like this:
description memberTag
one x
two (null)
当我尝试使用SQLite Expert Personal将(空)更改为 y时,它将引发异常。
And when I try to change (null) to "y" using SQLite Expert Personal, it throws the exception.
推荐答案
问题出在WHEN子句中:数据库不知道 memberTag
的来源,因为可能有两个行,
The problem is in the WHEN clause: the database does not know where memberTag
comes from, because there are two possible rows, the old one, and the new one.
使用 OLD.memberTag
或 NEW .memberTag
。
(还有另一个问题:UPDATE将更改表中的 all 行,因为忘记了WHERE子句。)
(There is another problem: the UPDATE will change all rows in the table, because you forgot the WHERE clause.)
这篇关于sqlite触发器导致“无此列”例外的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!