Does MySQL permit callbacks in C such that when a change happens, I can be notified?(MySQL 是否允许在 C 中进行回调,以便在发生更改时可以通知我?)
问题描述
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.
推荐答案
像这样创建触发器.
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 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 ;
希望你会很高兴.
这篇关于MySQL 是否允许在 C 中进行回调,以便在发生更改时可以通知我?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:MySQL 是否允许在 C 中进行回调,以便在发生更改时可以通知我?
基础教程推荐
- SSMS 中的权限问题:“对象 'extended_properties'、数据库 'mssqlsystem_resource'、... 错误 229)上的 SELECT 权限被拒绝" 2022-01-01
- 需要 MySQL 5.1 中的抽象触发器来更新审计日志 2021-01-01
- 在 SQL 中连接多个表 2021-01-01
- SQL Server 实例在登录协商期间返回无效或不受支持的协议版本 2021-01-01
- 是否可以执行按位分组功能? 2021-01-01
- 如何使用 mysql.connector 禁用查询缓存 2022-01-01
- 无法解决整理冲突 2021-01-01
- SQL 效率:WHERE IN 子查询 vs. JOIN 然后 GROUP 2021-01-01
- 将 SQL Server DateTime 列迁移到 DateTimeOffset 2021-01-01
- SQL:使用来自具有相同列名的两个表中的数据... 2021-01-01
