【问题标题】:java.sql.SQLException: Row 1 was truncated; it contained more data than there were input columnsjava.sql.SQLException:第 1 行被截断;它包含的数据多于输入列
【发布时间】:2015-02-19 12:46:44
【问题描述】:

我正在使用以下语法创建一个包含行的 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 终止,但没有运气。

【问题讨论】:

    标签: java mysql csv


    【解决方案1】:

    我相信你只需要添加@col5。定义 @col5 后可以忽略它,但错误是有效的(您的示例有 5 列,而不是 4 列)。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-09
      • 2013-04-20
      • 2014-01-09
      • 2021-03-14
      • 1970-01-01
      相关资源
      最近更新 更多