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!
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.
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.
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.
Python can be used in database applications. One of the most popular databases is MySQL.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With