How to duplicate INSERTS into a separate table?

落爺英雄遲暮 提交于 2019-12-25 05:19:48

问题


An iPhone application I have installed uses an SQLite database to log entries with basic INSERTS and DELETES being performed on it.

However I wish to keep a permanent log of the INSERTS made to this table, so when an INSERT occurs, I want it to be written to another table also so as to create a log.

I don't have access to the application source code in order to modify the SQL statements made, but I do have access to the SQLite database.

Can I do this with triggers? If so can somebody provide a short example.


回答1:


Never used SQLite, but here is the first link from google: http://www.sqlite.org/lang_createtrigger.html

you could write something like this probably:

CREATE TRIGGER duplicate_insert INSERT ON myTable
    BEGIN
        INSERT INTO myDuplicateTable
        VALUES(new.Id, new.Name, new.LastModified)
    END;

HTH




回答2:


Which part of the SQLite reference that you reach with Google and the search terms 'sqlite trigger' (namely SQLite Query Language: CREATE TRIGGER) are you having difficulty understanding?

CREATE TRIGGER ins_maintable AFTER INSERT ON MainTable
    FOR EACH ROW BEGIN INSERT INTO LoggingTable VALUES(NEW.Column1, ...); END;

Untested code...if the syntax diagram is to be believed, both semi-colons are needed; or, at least, the one before the END is needed.



来源:https://stackoverflow.com/questions/6489234/how-to-duplicate-inserts-into-a-separate-table

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!