[英]Is it possible to trigger a script or program if any data is updated in a database, like MySQL?
It doesn't have to be exactly a trigger inside the database. 它不必完全是数据库内部的触发器。 I just want to know how I should design this, so that when changes are made inside MySQL or SQL server, some script could be triggered.
我只想知道我应该如何设计它,以便在MySQL或SQL Server中进行更改时,可以触发一些脚本。
One Way would be to keep a counter on the last updated row in the database, and then you need to keep polling(Checking) the database through python for new records in short intervals. 一种方法是在数据库中最后更新的行上保留一个计数器,然后您需要在短时间内通过python不断轮询(检查)数据库以查找新记录。
If the value in the counter is increased then you could use the subprocess
module to call another Python script. 如果计数器中的值增加,则可以使用
subprocess
模块来调用另一个Python脚本。
It's possible to execute an external script from a MySql trigger, but I never used it and I don't know the implications of something like this. 可以从MySql触发器执行外部脚本,但是我从未使用过它,也不知道类似这样的含义。
MySql provides a way to implement your own functions, its called User Defined Functions . MySql提供了一种实现自己的功能的方法,称为用户定义函数 。 With this you can define your own functions and call them from MySql events.
这样,您可以定义自己的函数并从MySql事件中调用它们。 You need to write your own logic in a C program by following the interface provided by MySql.
您需要按照MySql提供的接口在C程序中编写自己的逻辑。
Fortunately someone already did a library to call an external program from MySql: LIB_MYSQLUDF_SYS . 幸运的是,已经有人编写了一个库来从MySql调用外部程序: LIB_MYSQLUDF_SYS 。 After installing it, the following trigger should work:
安装后,以下触发器应起作用:
CREATE TRIGGER Test_Trigger
AFTER INSERT ON MyTable
FOR EACH ROW
BEGIN
DECLARE cmd CHAR(255);
DECLARE result int(10);
SET cmd=CONCAT('/YOUR_SCRIPT');
SET result = sys_exec(cmd);
END;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.