简体   繁体   English

在SQLite触发器中更新多行

[英]Update Multiple Rows in SQLite Trigger

Is it possible to update multiple rows in an SQLite Trigger? 是否可以在SQLite触发器中更新多行? See below for my trigger. 请参阅下面的触发器。

CREATE TRIGGER "saved_tiles_reference"
 BEFORE DELETE ON favourites
FOR EACH ROW
BEGIN
    UPDATE saved_tiles SET reference_count = reference_count - 1 WHERE id = (select tile from favourite_tiles where favourite = OLD.id);
    DELETE FROM favourite_tiles WHERE id = OLD.id;
END;

As you can see I want to reduce the reference count on saved_tiles by 1. The saved_tiles I want to update are defined using the select statement in the where clause. 如您所见,我想将save_tiles的引用计数减少1。要更新的save_tiles是使用where子句中的select语句定义的。

This trigger works but for only the first entry it comes across. 该触发器有效,但仅适用于它遇到的第一个条目。

Edit 编辑

saved_tiles belong to its parent favourites and they're joined by the table favourite_tiles . saved_tiles属于其父favourites并且由表favourite_tiles加入。 Here is my database structure 这是我的数据库结构

-- ----------------------------
--  Table structure for favourite_tiles
-- ----------------------------
DROP TABLE IF EXISTS "favourite_tiles";
CREATE TABLE "favourite_tiles" (
     "id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
     "favourite" integer NOT NULL,
     "tile" integer NOT NULL
     CONSTRAINT "fk_favourite" FOREIGN KEY ("favourite") REFERENCES "favourites" ("id"),
     CONSTRAINT "fk_tiles" FOREIGN KEY ("tile") REFERENCES "saved_tiles" ("id")
);
INSERT INTO "main".sqlite_sequence (name, seq) VALUES ("favourite_tiles", '2');

-- ----------------------------
--  Table structure for favourites
-- ----------------------------
DROP TABLE IF EXISTS "favourites";
CREATE TABLE "favourites" (
     "id" integer NOT NULL,
    PRIMARY KEY("id")
);

-- ----------------------------
--  Table structure for saved_tiles
-- ----------------------------
DROP TABLE IF EXISTS "saved_tiles";
CREATE TABLE "saved_tiles" (
     "id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
     "reference_count" integer NOT NULL DEFAULT 0
);
INSERT INTO "main".sqlite_sequence (name, seq) VALUES ("saved_tiles", '1');

-- ----------------------------
--  Triggers structure for table favourite_tiles
-- ----------------------------
CREATE TRIGGER "increment_tile_reference"
 BEFORE INSERT ON favourite_tiles
FOR EACH ROW
BEGIN
-- Type the SQL Here.
    UPDATE saved_tiles SET reference_count = reference_count + 1 WHERE id = NEW.tile;
END;

-- ----------------------------
--  Triggers structure for table favourites
-- ----------------------------
CREATE TRIGGER "saved_tiles_reference"
 BEFORE DELETE ON favourites
FOR EACH ROW
BEGIN
-- Type the SQL Here.
    UPDATE saved_tiles SET reference_count = reference_count - 1 WHERE id = (select tile from favourite_tiles where favourite = OLD.id);
    DELETE FROM favourite_tiles WHERE id = OLD.id;
END;

Unfortunately I solved it. 不幸的是我解决了。

Instead of using id = (select.... , I used id IN (select ... as follows in the modified trigger. 在修改后的触发器中,我使用id IN (select ... ,而不是使用id = (select....

CREATE TRIGGER "saved_tiles_reference"
 BEFORE DELETE ON favourites
FOR EACH ROW
BEGIN
    UPDATE saved_tiles SET reference_count = reference_count - 1 WHERE id IN (select tile from favourite_tiles where favourite = OLD.id);
    DELETE FROM favourite_tiles WHERE favourite = OLD.id;
END;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM