执行Python脚本的MySQL触发器

时间:2019-06-12 16:33:56

标签: python mysql windows triggers automation

我有一个Python脚本,它可以: 1.读取纬度和经度的MySQL input_table, 2.使用OpenWeatherMap API计算weather_data。 3.将此weather_data插入weather_table

每次我在Windows命令提示符上手动运行该Python脚本时,它都会执行所需的操作。查询是:如何确保每次将新记录插入input_table中时都会运行此.py脚本(而无需我手动运行.py脚本)?

为此,我编写了一个MySQL触发器:

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)

要测试触发器,我的工作流程是: a)将1条记录插入input_table中 b)在weather_table

中检查相应的结果

现在,当我在input_table中插入1条记录时,MySQL Workbench上的消息受到'1行影响'。但是,weather_table没有变化。是否因为我宣布result的数据类型为int' but the result`会返回一条记录,如下所示:


datatypes of columns in the weather_table

总体问题是:每当将新记录插入到input_table中时,如何确保此.py脚本运行完成(成功将weather-table插入INSERTS)(而无需我手动运行)。 py脚本)?

0 个答案:

没有答案