用于更新编辑列的SQLite触发器更新表中所有记录的列

时间:2018-04-10 17:10:48

标签: sqlite triggers

假设表x,包含列(所有类型text):

  • 名称
  • SNAME
  • sqlmodded

如果记录中的任何列都已更新,我想创建一个触发器,将sqlmodded的值设置为“1”。

我的触发器如下:

CREATE TRIGGER trigger1
AFTER UPDATE
ON x
FOR EACH ROW
      WHEN old.sqlmodded IS NULL
BEGIN
    UPDATE x
       SET sqlmodded = '1';
END;

当我运行更新语句来更改namesname的值时,触发器会启动,但会更改表中所有记录的sqlmodded

以下是重现的代码:

CREATE TABLE "x"(
"NAME" Text,
"SNAME" Text,
"sqlmodded" Text );

CREATE TRIGGER "trigger1"
    AFTER UPDATE
    ON "x"
    FOR EACH ROW
    WHEN old.sqlmodded is null
BEGIN UPDATE x SET sqlmodded = '1'; END;

现在插入一些记录:

INSERT INTO x
    (
      name
    , sname
    ) VALUES
    (
      "Joe"
    , "Bloggs"
    )
    ;

INSERT INTO x
(
  name
, sname
) VALUES
(
  "Joline"
, "Bloggs"
)
;

现在运行更新:

UPDATE x
SET
  name = "Justine"
WHERE name = "Joline"
;

如果您查看生成的两条记录,则两条记录都会将sqlmodded设置为1。

2 个答案:

答案 0 :(得分:0)

触发器运行此命令:

import tkinter as tk
from tkinter import ttk
from tkinter import PhotoImage
from PIL import Image, ImageTk

Large_Font = ("Times", 20)

class RobProject(tk.Toplevel):

    def __init__(self):

        tk.Toplevel.__init__(self)

        tk.Tk.wm_title(self, "Com-Bot Control")

        container = tk.Frame(self)
        container.pack(side="top", fill="both", expand = True)
        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1)

        self.frames = {}

        for F in (MenuPage, PageOne, PageTwo, PageThree):

            frame = F(container, self)

            self.frames[F] = frame

            frame.grid(row=0, column=0, sticky="NEWS")

        self.show_frame(MenuPage)

    def show_frame(self, cont):

        frame = self.frames[cont]
        frame.tkraise()

class MenuPage(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self,parent)

        label = tk.Label(self, text = "Menú", font=Large_Font)
        label.pack(pady=10, padx=10)

        RCB = ttk.Button(self, text="Control Remoto",
                        command= lambda: controller.show_frame(PageOne))
        RCB.pack()

        EOB = ttk.Button(self, text="EvitaObstáculos",
                        command= lambda: controller.show_frame(PageTwo))
        EOB.pack()

        SLB = ttk.Button(self, text="Siguelíneas",
                        command= lambda: controller.show_frame(PageThree))
        SLB.pack()

        img = Image.open("play-button.png")
        photo = ImageTk.PhotoImage(img)

        Show_Im = tk.Button(self, image=photo)
        Show_Im.image = photo
        Show_Im.pack()

class PageOne(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)

        label = tk.Label(self, text = "Control Remoto", font=Large_Font)
        label.pack(pady=10, padx=10)

        Back_B1 = ttk.Button(self, text="Atrás",
                        command= lambda: controller.show_frame(MenuPage))
        Back_B1.pack()

class PageTwo(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)

        label = tk.Label(self, text = "Evitaobstáculos", font=Large_Font)
        label.pack(pady=10, padx=10)

        Back_B2 = ttk.Button(self, text="Atrás",
                        command= lambda: controller.show_frame(MenuPage))
        Back_B2.pack()        

class PageThree(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)

        label = tk.Label(self, text = "Siguelíneas", font=Large_Font)
        label.pack(pady=10, padx=10)

        Back_B3 = ttk.Button(self, text="Atrás",
                        command= lambda: controller.show_frame(MenuPage))
        Back_B3.pack()

app = RobProject()
app.mainloop()

此语句确实更新了表中的所有行。这就是SQL的工作原理。

如果只想更新特定行,则必须告诉数据库将是哪一行:

UPDATE x
   SET sqlmodded = '1';

答案 1 :(得分:0)

这是工作代码:

CREATE TRIGGER sqlmods
         AFTER UPDATE
            ON x
      FOR EACH ROW
          WHEN old.sqlmodded IS NULL
BEGIN
    UPDATE x
       SET sqlmodded = TRUE
     WHERE rowid = NEW.rowid;
END;