插入触发器不工作SQLite

时间:2016-10-12 09:39:58

标签: sql sqlite triggers

我有审计表来维护数据库中的所有操作。我有TestDetails表并在其上创建INSERT,UPDATE触发器。更新触发器在windows7中工作正常,INSERT触发器不能与windows7一起使用。但是,当我在Windows 8,10中测试插入触发器时,它运行良好。我已将查询包括在内:

 CREATE TABLE TestDetails (
        ID           INTEGER      PRIMARY KEY AUTOINCREMENT
                                  UNIQUE
                                  NOT NULL,
        Type  VARCHAR (50),
       CreatedDate DATETIME, ModifiedDate DATETIME, CreatedBy INT, ModifiedBy INT
    );
    CREATE TABLE Audit (
        TableName VARCHAR (50),
        ID        INT (10),
        Type      VARCHAR (30),
        CDT       DATETIME,
        Changeset TEXT (800) 
    );

    INSERT TRIGGER :
    CREATE TRIGGER TRGINS_TestDetails AFTER INSERT ON TestDetails FOR EACH ROW  BEGIN  INSERT INTO Audit(TableName,ID,Type,CDT,ChangeSet) VALUES('TestDetails', new.ID, 'INSERT',DATETIME('NOW'), 
    '<ChangeSet>' || 
    '<Id><New>' || new.Id || '</New>' || 
    '<Type><New>' || new.Type || '</New>' ||
'<CreatedDate><New>' || new.CreatedDate || '</New>' ||  
'<CreatedBy><New>' || new.CreatedBy || '</New>' || 
'<ModifiedDate><New>' || new.ModifiedDate || '</New>' ||  
'<ModifiedBy><New>' || new.ModifiedBy || '</New>' || 
     || '</ChangeSet>'); END


    UPDATE TRIGGER :
    CREATE TRIGGER [TRGUPD_TestDetails ]
             AFTER UPDATE
                ON TestDetails
    BEGIN INSERT INTO Audit ( TableName, ID,Type, CDT, ChangeSet )
                               VALUES ('TestDetails', new.ID,'UPDATE',DATETIME('NOW'),
                                   '<ChangeSet>' || CASE WHEN old.Id <> new.Id THEN '<Id><Old>' || old.Id || '</Old><New>' || new.Id || '</New>' ELSE '' END || CASE WHEN old.Type <> new.Type THEN '<Type><Old>' || old.Type || '</Old><New>' || new.Type || '</New>' ELSE '' END || 
CASE WHEN old.CreatedDate <> new.CreatedDate THEN '<CreatedDate><Old>' || old.CreatedDate || '</Old><New>' || new.CreatedDate || '</New>' ELSE '' END || 
 CASE WHEN old.ModifiedDate <> new.ModifiedDate THEN '<ModifiedDate><Old>' || old.ModifiedDate || '</Old><New>' || new.ModifiedDate || '</New>' ELSE '' END || 
 CASE WHEN old.CreatedBy <> new.CreatedBy THEN '<CreatedBy><Old>' || old.CreatedBy || '</Old><New>' || new.CreatedBy || '</New>' ELSE '' END || 
 CASE WHEN old.ModifiedBy <> new.ModifiedBy THEN '<ModifiedBy><Old>' || old.ModifiedBy || '</Old><New>' || new.ModifiedBy || '</New>' ELSE '' END ||
'</ChangeSet>'  );END;

Audit : 
TableName      ID       Type    CDT                      ChangeSet
TestDetails 1   INSERT  2016-10-12 09:03:00 NULL
TestDetails 1   UPDATE  2016-10-12 09:05:32 <ChangeSet><Type><Old>Type1</Old><New>Type11</New></ChangeSet>

The actual value for Changeset in windows8,10 is : <ChangeSet><ID><New>1</New><Type><New>Type1</New></ChangeSet>

任何人都可以帮我解决这个问题吗? 提前谢谢。

1 个答案:

答案 0 :(得分:0)

我找到了解决方案。问题是,虽然插入我们没有维护ModifiedDate,Modified By。所以,在触发器中我们不需要维护那些字段。我改变了我的触发器如下:

 CREATE TRIGGER TRGINS_TestDetails AFTER INSERT ON TestDetails FOR EACH ROW  BEGIN  INSERT INTO Audit(TableName,ID,Type,CDT,ChangeSet) VALUES('TestDetails', new.ID, 'INSERT',DATETIME('NOW'), 
    '<ChangeSet>' || 
    '<Id><New>' || new.Id || '</New>' || 
    '<Type><New>' || new.Type || '</New>' ||
'<CreatedDate><New>' || new.CreatedDate || '</New>' ||  
'<CreatedBy><New>' || new.CreatedBy || '</New>'  
     || '</ChangeSet>'); END

现在正在努力......谢谢你们......