简体   繁体   English

如果在数据库(如MySQL)中有任何数据更新,是否可以触发脚本或程序?

[英]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.

相关问题 将数据从python脚本添加到mysql数据库 - Adding data from python script into mysql database 如何定期将数据从Python程序插入MySQL数据库 - How to insert data from Python program to MySQL database at intervals MySQL 执行 Python 脚本的触发器 - MySQL Trigger that executes a Python script 在Mysql数据库服务器中触发触发器时,可以在树莓派上运行python脚本吗? - Can i run a python script on my raspberry pi when a trigger is triggered in Mysql database server? python脚本中的INSERT QUERY错误,无法将数据插入MySQL数据库 - Error with INSERT QUERY from python script to insert data into MySQL database 从php运行python脚本并在mysql数据库中写入数据值 - Running a python script from php and writing data values in mysql database 在将数据从python脚本插入MySQL数据库时需要指导 - Need guidance in inserting data from python script into a MySQL database 通过SimpleHTTPServer调用python脚本将数据插入MYSQL数据库 - Insert data into MYSQL Database by invoking python script through SimpleHTTPServer javascript代码是否有可能获取正在运行的python程序的不断更新的变量? - Is it possible for a javascript code to get the constantly updated variables of a running python program? 是否可以将 Excel 与 MySQL 数据库连接起来 - Is it possible to connect an Excel with a MySQL Database
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM