SQLite3 无法删除行,没有这样的列错误
SQLite3 can't DELETE row, no such column Error
我有一个简单的 sqlite 数据库设置,其中有一个名为 sightings 的 table 和一个名为 name 的 table 列。我可以 运行 正常 SELECT 查询和插入和更新就好了,但是当我尝试 运行 删除时,它会在我给它的列中添加 "new." 然后说它不存在:
sqlite> DELETE FROM sightings WHERE name = "blah";
Error: no such column: new.name
sqlite> DELETE FROM sightings WHERE names = "blah";
Error: no such column: names
该列名为 "name",当我 select 该列时,它会在其前面添加 "new.",但是当我 select 一个错误的列时,它不会't exist ("names" 如上),它只是说它通常不存在,没有 "new."。
如有任何帮助,我们将不胜感激。
您似乎有一个 DELETE TRIGGER,并且该 TRIGGER 错误地使用 new.name 来引用已删除行的名称列(对于 DELETE TRIGGER old.is用于引用已删除行中的列)。
您应该将 TRIGGER 更改为使用 old.name 而不是 new.name.
new.column 仅适用于 INSERT 或 UPDATE 触发器。
old.column 仅适用于 DELETE 或 UPDATE 触发器。
根据 :-
Both the WHEN clause and the trigger actions may access elements of
the row being inserted, deleted or updated using references of the
form "NEW.column-name" and "OLD.column-name", where column-name is the
name of a column from the table that the trigger is associated with.
OLD and NEW references may only be used in triggers on events for
which they are relevant, as follows:
- INSERT NEW references are valid
- UPDATE NEW and OLD references are
valid
- DELETE OLD references are valid
SQL As Understood By SQLite - CREATE TRIGGER
也许考虑下面的例子:-
CREATE TABLE IF NOT EXISTS sightings (name TEXT);
CREATE TABLE IF NOT EXISTS deleted_sightings (name TEXT); /* Table to be populated by the trigger */
INSERT INTO sightings VALUES ('blah'),('notblah'),('anothernotblah');
/* Normal deletion without triggers */
DELETE FROM sightings WHERE name = 'blah';
SELECT * FROM sightings; /* RESULT 1 (sightings table after deletion)*/
/* Add the row that was deleted again */
INSERT INTO sightings VALUES('blah');
/* Add a valid AFTER DELETE TRIGGER referring to the old column */
/* The Trigger will add a row to the deleted_sightings table using the value from the deleted row */
CREATE TRIGGER IF NOT EXISTS correct_trigger
AFTER DELETE ON sightings
BEGIN INSERT INTO deleted_sightings
VALUES(old.name);
END;
DELETE FROM sightings WHERE name = 'blah';
SELECT * FROM sightings; /* RESULT 2 (sightings table after deletion)*/
SELECT * FROM deleted_sightings; /* RESULT 3 deleted_sightings table */
/* Add a Trigger that will try to add a row to the deleted_sightings table */
/* BUT will result in the new.name column not being found as there is no */
/* new. for a DELETE trigger only old. */
CREATE TRIGGER IF NOT EXISTS incorrect_trigger AFTER DELETE ON sightings BEGIN INSERT INTO deleted_sightings VALUES(new.name); END;
/* Show the triggers */
SELECT * FROM sqlite_master WHERE type = 'trigger'; /* RESULT 4 - The triggers as per sqlite_master */
DELETE FROM sightings WHERE name = 'blah'; /* <<<<< DELETE will fail due to incorrect trigger */
结果是:-
以及由于不正确的触发器导致的错误:-
/* RESULT 4 - The triggers as per sqlite_master */
DELETE FROM sightings WHERE name = 'blah'
> no such column: new.name
> Time: 0s
我有一个简单的 sqlite 数据库设置,其中有一个名为 sightings 的 table 和一个名为 name 的 table 列。我可以 运行 正常 SELECT 查询和插入和更新就好了,但是当我尝试 运行 删除时,它会在我给它的列中添加 "new." 然后说它不存在:
sqlite> DELETE FROM sightings WHERE name = "blah";
Error: no such column: new.name
sqlite> DELETE FROM sightings WHERE names = "blah";
Error: no such column: names
该列名为 "name",当我 select 该列时,它会在其前面添加 "new.",但是当我 select 一个错误的列时,它不会't exist ("names" 如上),它只是说它通常不存在,没有 "new."。 如有任何帮助,我们将不胜感激。
您似乎有一个 DELETE TRIGGER,并且该 TRIGGER 错误地使用 new.name 来引用已删除行的名称列(对于 DELETE TRIGGER old.is用于引用已删除行中的列)。
您应该将 TRIGGER 更改为使用 old.name 而不是 new.name.
new.column 仅适用于 INSERT 或 UPDATE 触发器。
old.column 仅适用于 DELETE 或 UPDATE 触发器。
根据 :-
Both the WHEN clause and the trigger actions may access elements of the row being inserted, deleted or updated using references of the form "NEW.column-name" and "OLD.column-name", where column-name is the name of a column from the table that the trigger is associated with. OLD and NEW references may only be used in triggers on events for which they are relevant, as follows:
- INSERT NEW references are valid
- UPDATE NEW and OLD references are valid
- DELETE OLD references are valid
SQL As Understood By SQLite - CREATE TRIGGER
也许考虑下面的例子:-
CREATE TABLE IF NOT EXISTS sightings (name TEXT);
CREATE TABLE IF NOT EXISTS deleted_sightings (name TEXT); /* Table to be populated by the trigger */
INSERT INTO sightings VALUES ('blah'),('notblah'),('anothernotblah');
/* Normal deletion without triggers */
DELETE FROM sightings WHERE name = 'blah';
SELECT * FROM sightings; /* RESULT 1 (sightings table after deletion)*/
/* Add the row that was deleted again */
INSERT INTO sightings VALUES('blah');
/* Add a valid AFTER DELETE TRIGGER referring to the old column */
/* The Trigger will add a row to the deleted_sightings table using the value from the deleted row */
CREATE TRIGGER IF NOT EXISTS correct_trigger
AFTER DELETE ON sightings
BEGIN INSERT INTO deleted_sightings
VALUES(old.name);
END;
DELETE FROM sightings WHERE name = 'blah';
SELECT * FROM sightings; /* RESULT 2 (sightings table after deletion)*/
SELECT * FROM deleted_sightings; /* RESULT 3 deleted_sightings table */
/* Add a Trigger that will try to add a row to the deleted_sightings table */
/* BUT will result in the new.name column not being found as there is no */
/* new. for a DELETE trigger only old. */
CREATE TRIGGER IF NOT EXISTS incorrect_trigger AFTER DELETE ON sightings BEGIN INSERT INTO deleted_sightings VALUES(new.name); END;
/* Show the triggers */
SELECT * FROM sqlite_master WHERE type = 'trigger'; /* RESULT 4 - The triggers as per sqlite_master */
DELETE FROM sightings WHERE name = 'blah'; /* <<<<< DELETE will fail due to incorrect trigger */
结果是:-
以及由于不正确的触发器导致的错误:-
/* RESULT 4 - The triggers as per sqlite_master */ DELETE FROM sightings WHERE name = 'blah' > no such column: new.name > Time: 0s