简体   繁体   中英

MySQL Trigger that executes a Python script

I have a Python script that: 1. reads the MySQL input_table of latitudes and longitudes, 2. computes weather_data using OpenWeatherMap API. 3. INSERTS this weather_data into a weather_table

This Python script does what is needed everytime I manually run it on my Windows Command Prompt. The query is: how do I ensure that this.py script runs everytime a new record is INSERTed into my input_table (without me having to manually run the.py script)?

To this effect, I wrote a MySQL trigger:

DELIMITER $$
CREATE TRIGGER ds_testdb1.weather_trigger_1 AFTER INSERT ON ds_testdb1.input_table 
FOR EACH ROW 
BEGIN
DECLARE cmd TEXT;
DECLARE result int;
SET cmd= 'python C:/Users/SS19/Desktop/insert_weather.py';
SET result= sys_exec(cmd);       -- is a UDF (User Defined Function)
END $$
DELIMITER ;

-- Query OK, 0 rows affected (0.10 sec)

To test the trigger, my workflow is: a) INSERT 1 record into the input_table b) check the corresponding result in the weather_table

Now, when I Insert 1 record into the input_table , the message on MySQL Workbench is '1 row(s) affected'. However, no change is seen the the weather_table . Is it because I declared the datatype of result to be int' but the result` returns a record that looks like:


weather_table 中列的数据类型

The Overarching question is: how do I ensure that this.py script runs to completion (successfully performs INSERTS into the weather-table ) everytime a new record is INSERTed into my input_table (without me having to manually run the.py script)?

Try to change "SET cmd= 'python C:/Users/SS19/Desktop/insert_weather.py';" with "SET cmd= 'C:/..full path to python.exe../python C:/Users/SS19/Desktop/insert_weather.py';".

It worked for me.

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