简体   繁体   中英

java.sql.SQLException: Row 1 was truncated; it contained more data than there were input columns

I am creating a CSV file with rows using this syntax:

  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');

I then try to load the csv files into mysql using this syntax:

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

But I get the error:

java.sql.SQLException: Row 1 was truncated; it contained more data than there were input columns

Looking online it seems like a delimiter problem. I am using Linux ubuntu so I have tried to terminate by \\n and also \\r\\n with no luck.

I believe you just need to add @col5 . You can ignore @col5 after you define it, but the error is valid (your sample has 5 columns, not 4).

"(@col1,@col2,@col3,@col4,@col5)"

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM