2012-03-28 71 views
0

請幫我一把。我將一個字符串插入到mysql數據庫表中的列中。但由於我使用的是預定義的API來插入表格,我需要一個觸發器來幫助將字符串解析到其餘列中。這個mySQL觸發語法是否正確?

字符串在長相未來這樣的:((管理員))[[的clientId]] >>>更新客戶資料

該字符串將被插入到該列 '消息'。但是我想在該表中插入3個其他列:user_id,client_id &操作。所以我需要將消息解析爲3個不同的字符串,以便將它們放入正確的列中。

這是我有,這是正確的?

CREATE TRIGGER 'parse_log4j_message' BEFORE INSERT ON 'util_audit' 
FOR EACH ROW BEGIN 
select NEW.MESSAGE into @message; //adding String into this variable 
    select LEFT(message, 1) from table into @firstChar; //get first Char in Message 
IF strcmp(@firstChar,'(') = 0 THEN //if first char is '(', then message is correct 
    SELECT SUBSTRING_INDEX(@message, ')', 2) into @userId; //-> admin 
    SELECT SUBSTRING_INDEX(@message, ']', 2) into @tempClientId; 
    SELECT SUBSTRING_INDEX(@message, '[', -1) into @clientId; //-> clientId 
    SELECT SUBSTRING_INDEX(@message, '>', -1) into @action; //-> the rest of the message 
    SET NEW.USER_ID = @userId; // set into the columns of the table 
    SET NEW.CLIENT_ID = @clientId; 
    SET NEW.ACTION = @action; 
END IF; 
END; 

回答

2

試試這個解析腳本首先 -

SET @message = '((admin)) [[clientId]] >>>Update client Profile'; 

SET @userId = SUBSTRING_INDEX(SUBSTRING_INDEX(@message, '))', 1), '((', -1); 
SET @clientId = SUBSTRING_INDEX(SUBSTRING_INDEX(@message, ']]', 1), '[[', -1); 
SET @action = SUBSTRING_INDEX(@message, '>>>', -1); 

SELECT @userId, @clientId, @action; 

+---------+-----------+-----------------------+ 
| @userId | @clientId | @action    | 
+---------+-----------+-----------------------+ 
| admin | clientId | Update client Profile | 
+---------+-----------+-----------------------+ 

如果這是正確的,那麼這個解決方案添加到您的觸發器。