简体   繁体   中英

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.

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.
If the value in the counter is increased then you could use the subprocess module to call another Python script.

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 provides a way to implement your own functions, its called User Defined Functions . With this you can define your own functions and call them from MySql events. You need to write your own logic in a C program by following the interface provided by MySql.

Fortunately someone already did a library to call an external program from 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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM