我正在使用以下语法创建包含行的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终止而没有运气。
答案 0 :(得分:1)
我相信你只需要添加@col5
。您可以在定义@col5
后忽略它,但错误有效(您的样本有5列,而不是4列)。
"(@col1,@col2,@col3,@col4,@col5)"