我看了http://crazytechthoughts.blogspot.sg/2011/12/call-external-program-from-mysql.html并安装了lib_mysqludf_sys
并尝试在mysql触发器中调用python脚本。
这是我的测试触发器:
DROP TRIGGER IF EXISTS ljj_rigger;
CREATE TRIGGER ljj_rigger
AFTER INSERT ON test_trigger
FOR EACH ROW
BEGIN
DECLARE cmd1 CHAR(255);
DECLARE cmd2 CHAR(255);
DECLARE cmd3 CHAR(255);
DECLARE result1 int(10);
DECLARE result2 int(10);
DECLARE result3 int(10);
SET cmd1 = 'python /home/jaskey1103/python/test.py';
SET cmd2 = 'touch /home/jaskey1103/python/bybash';
SET cmd3 = '/home/jaskey1103/python/callpy.sh';
SET result1 = sys_exec(cmd1);
SET result2 = sys_exec(cmd2);
SET result3 = sys_exec(cmd3);
insert into test_trigger_copy(value) values(result1);
insert into test_trigger_copy(value) values(result2);
insert into test_trigger_copy(value) values(result3);
END;
commit;
但是当触发器运行时,我可以找到新文件bytouch
,但无法找到python脚本创建的新文件。
为了进行测试,将复制表中的插入结果为:0,0,512。
test.py:
import os
def touch(fname, times=None):
fhandle = open(fname, 'a')
try:
os.utime(fname, times)
finally:
fhandle.close()
print 'creating file'
touch("bypython")
callpy.sh:
python test.py