我正在使用以下语法创建包含行的 CSV 文件:
writer.append("SomeString");
writer.append(',');
writer.append("SomeValue");
writer.append(',');
writer.append("SomeString");
writer.append(',');
writer.append("SomeValue");
writer.append(',');
writer.append("SomeOtherValue");
writer.append('\n');
然后我尝试使用以下语法将 csv 文件加载到 mysql 中:
String loadFileSQL = "LOAD DATA INFILE '/home/CSV_FILES/"
+ f.getName().substring(0, f.getName().length() - 4)
+ ".csv' "
+ "INTO TABLE counts "
+ "FIELDS TERMINATED BY ',' "
+ "LINES TERMINATED BY '\n' "
+ "(@col1,@col2,@col3,@col4)"
+ "SET pstr=@col1, pcnt=@col2, rstr=@col3, rcnt=@col4;";
但是我得到了错误:
java.sql.SQLException: Row 1 was truncated; it contained more data than there were input columns
网上看好像是分隔符的问题。我正在使用 Linux ubuntu,所以我尝试通过\n 和\r\n 终止,但没有成功。
最佳答案
我相信你只需要添加@col5
。您可以在定义后忽略 @col5
,但错误是有效的(您的样本有 5 列,而不是 4)。
"(@col1,@col2,@col3,@col4,@col5)"
关于java.sql.SQLException : Row 1 was truncated; it contained more data than there were input columns 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27585411/