LOAD DATA INFILE MYSQL错误

时间:2014-02-15 19:16:29

标签: java mysql

我想使用load data infile命令在mysql表中加载一个文件thrugh java program但是我收到了MySQL语法错误:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1

sql语句如下:

 String sql =    "LOAD DATA  LOCAL INFILE '/Dataset/"+listOfFiles[i].getName()+".txt'" +
                            "    INTO TABLE data " +
                            "    CHARACTER SET utf8 " +
                            "    FIELDS " +
                            "        TERMINATED BY '|' " +
                            "    LINES " +
                            "    STARTING BY 'status' " +
                            "        TERMINATED BY '\n' " +
                            "    (@col_type, @col_origin, @col_text, @col_url, @col_id, @col_time, @col_retcount)"+
                            "     set userid = '1234' "
                            "      , original = @col_origin "+
                            "      , datatext = @col_text "+
                            "      , url = @col_url "+
                            "      , id = @col_id "+
                            "      , retcount = @col_retcount "+                                
                            "      , date = str_to_date(@col_time, '%m/%d/%Y') "+
                            "      , isanswer = CASE WHEN @col_origin LIKE '% abcd %' THEN 1 ELSE 0 END ";

我的问题是我无法发现错误。 有谁可以帮助我?

1 个答案:

答案 0 :(得分:0)

您需要在Java字符串附近有两个反斜杠(\)

"       Terminated By '\\n' " +
                        ^ new

然后,你的字符串看起来像这样:

String sql = "LOAD DATA "
        + " LOCAL INFILE '/Dataset/"+listOfFiles[i].getName()+".txt'"
        + "    INTO TABLE data "
        + "    CHARACTER SET utf8 "
        + "    FIELDS "
        + "        TERMINATED BY '|' "
        + "    LINES "
        + "    STARTING BY 'status' "
        + "        TERMINATED BY '\\n' "
        + "    (@col_type, @col_origin, @col_text, @col_url, @col_id, @col_time, @col_retcount)"
        + "     set userid = '1234' "
        + "      , original = @col_origin "
        + "      , datatext = @col_text "
        + "      , url = @col_url "
        + "      , id = @col_id "
        + "      , retcount = @col_retcount "
        + "      , date = str_to_date(@col_time, '%m/%d/%Y') "
        + "      , isanswer = CASE WHEN @col_origin LIKE '% abcd %' THEN 1 ELSE 0 END ";