Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run python script on Database event

I'm running a python script that makes modifications in a specific database. I want to run a second script once there is a modification in my database (local server).

Is there anyway to do that?

Any help would be very appreciated. Thanks!

like image 521
HaTiMuX Avatar asked Apr 30 '14 07:04

HaTiMuX


People also ask

Can you run Python script in SQL?

To run a Python script, you'll pass it as an argument to the system stored procedure, sp_execute_external_script. This system stored procedure starts the Python runtime in the context of SQL machine learning, passes data to Python, manages Python user sessions securely, and returns any results to the client.

Can I run Python script in MySQL?

connector to connect Python Script to the MySQL database. Download the mysql. connector from here and install it on your computer. Now, check whether you have installed the mysql.

How do you call a Python function in SQL query?

To call this line of Python from T-SQL, add the Python function in the Python script parameter of sp_execute_external_script . The output expects a data frame, so use pandas to convert it. EXECUTE sp_execute_external_script @language = N'Python' , @script = N' import numpy import pandas OutputDataSet = pandas.

Can Python connect to DB?

Python can be used in database applications. One of the most popular databases is MySQL.


Video Answer


1 Answers

Thanks for your answers, i found a solution here:

http://crazytechthoughts.blogspot.fr/2011/12/call-external-program-from-mysql.html

A Trigger must be defined to call an external function once the DB Table is modified:

DELIMITER $
CREATE TRIGGER Test_Trigger
AFTER INSERT ON SFCRoutingTable
FOR EACH ROW
BEGIN
DECLARE cmd CHAR(255);
DECLARE result int(10);
SET cmd = CONCAT('python /home/triggers.py');
SET result = sys_exec(cmd);
END;
$
DELIMITER ;

Here, to call my python script, I use 'sys_exec' which is a UDF (User Defined Function). You can download the library from here: https://github.com/mysqludf/lib_mysqludf_sys

like image 167
HaTiMuX Avatar answered Sep 23 '22 18:09

HaTiMuX