I'd like to load a file in a mysql table thrugh java program using load data infile command but I am receiving MySQL Syntax Error:
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
The sql statement is the following:
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 ";
My problem is that I cannot spot the error. Could anyone help me?
You need to have two backslashes (\\) in your Java String near
" Terminated By '\\n' " +
^ new
Your String, then, would look like this:
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 ";
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.