MySQL Load Data Infile when conditions are met

拥有回忆 提交于 2019-12-13 01:09:34

问题


I am currently building a MySQL database. The database is storing data which is being supplied by another organisation and the format of the files we recieve is, shall we say, somewhat inconsistent.

I have writted a function which assess the file based on some simple rules and returns a message saying 'file ok' or else one of a list of hard coded error messages. The way I would like to use this function would be to say something like:

if check_10m_file() = 'file ok' then 
  load data infile '\\\\server\\filepath\file.csv'
  fields terminated by ','
  ...(etc)

My problem is that the if..then..else control structure does not seem to be allowed outside of stored procedures and the load data infile command is not allowed inside stored procedures.

I have attempted to trick my way around this by building the load data infile statement as a prepared statement but then I get an error

"This command is not supported in the prepared statement protocol yet"

So my question is, does anyone know of a way in which I can run a load data statement only when conditions are met? I would ideally like to put this into a stored procedure but if I could just save the code as a script to run later, that would be acceptable.


回答1:


One option is to use UDF, for example: lib_mysqludf_sys.

After installing the UDF, you can do something like:

Shell script (/server/loadpath/load.sh):

mysql -u [user] -p[pass] -e "LOAD DATA INFILE '$1' INTO TABLE $2;"

Stored Procedure:

DELIMITER $$

DROP PROCEDURE IF EXISTS load_data$$

CREATE PROCEDURE load_data(pfile VARCHAR(100), pdbtable VARCHAR(100))
BEGIN
    DECLARE exec_str VARCHAR(500);
    DECLARE ret_val INT;
    IF (check_10m_file() = 'file ok') THEN
        SET exec_str := CONCAT('sh /server/loadpath/load.sh ', pfile, ' ', pdbtable);
        SET ret_val := sys_exec(exec_str);
        IF ret_val = 0 THEN
            SELECT 'OK' Result;
        ELSE
            SELECT 'ERROR' Result;
        END IF;
    END IF;
END$$

DELIMITER ;

CALL load_data('/server/filepath/file.csv', 'mydb.mytable');

IMPORTANT: Validate the input data to prevent any code injection.



来源:https://stackoverflow.com/questions/19053263/mysql-load-data-infile-when-conditions-are-met

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!