2014-02-15 140 views
0

我想使用LOAD DATA INFILE命令加載一個文件在一個MySQL表thrugh Java程序,但我收到了MySQL的語法錯誤:LOAD DATA INFILE 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 "; 

我的問題是我無法發現錯誤。 任何人都可以幫助我嗎?

回答

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 ";