MySQL允许在C中的回调,以便当发生更改时,我可以通知? [英] Does MySQL permit callbacks in C such that when a change happens, I can be notified?
问题描述
MySQL允许C中的回调,以便在数据库中发生更改(如插入)时,由不同的程序或用户在命令行执行,我可以通知?
Does MySQL permit callbacks in C such that when a change happens in the database, like an insert, that is performed by a different program or by the user at the command line, I can be notified?
我猜想它没有,因为mysqlclient是一个库,不是一个正在运行的线程。
I am guessing that it doesn't, because mysqlclient is a library, not a running thread. But I may as well ask.
推荐答案
创建一个这样的触发器。
Create a trigger like so.
DELIMITER $$
CREATE TRIGGER ad_mytable_each AFTER DELETE ON MyTable FOR EACH ROW
BEGIN
#write code that trigger After delete (hence the "ad_" prefix)
#For table MyTable (The _MyTable_ middle)
#On each row that gets inserted (_each suffix)
#
#You can see the old delete values by accesing the "old" virtual table.
INSERT INTO log VALUES (old.id, 'MyTable', old.field1, old.field2, now());
END$$
DELIMITER ;
有 INSERT
, DELETE
, UPDATE
他们可以触发 BEFORE
或 AFTER
后的操作。
触发器 BEFORE
There are triggers for INSERT
, DELETE
, UPDATE
And they can fire BEFORE
or AFTER
the action.
The trigger BEFORE
the action can cancel the action by forcing an error, like so.
CREATE TRIGGER bd_mytable_each BEFORE DELETE ON MyTable FOR EACH ROW
BEGIN
#write code that trigger Before delete (hence the "db_" prefix)
declare DoError Boolean;
SET DoError = 0;
IF old.id = 1 THEN SET DoError = 1; END IF;
IF (DoError = 1) THEN SELECT * FROM Table_that_does_not_exist_to_force_error;
#seriously this example is in the manual.
END$$
DELIMITER ;
这将防止删除记录1.
This will prevent deletion of record 1.
A before UPDATE触发器甚至可以更改更新的值。
A before UPDATE Trigger can even change the values updated.
CREATE TRIGGER bu_mytable_each BEFORE UPDATE ON MyTable FOR EACH ROW
BEGIN
IF new.text = 'Doon sucks' THEN SET new.text = 'Doon rules';
END$$
DELIMITER ;
希望你会成为Trigger的快乐。
Hope you'll be Trigger happy.
这篇关于MySQL允许在C中的回调,以便当发生更改时,我可以通知?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!