如何在使用 LOAD DATA 命令时跳过 csv 文件的行?

发布于 2024-09-12 14:31:56 字数 236 浏览 2 评论 0原文

我正在使用 sql 命令加载数据将 csv 文件中的数据插入到 mysql 数据库中。问题是在文件末尾有几行像“,,,,,,,,,,,,,,,,,,”(csv文件是excel文件的转换)。因此,当 sql 到达这些行时,他向我发送:#1366 - 错误的整数值:第 661 行的“Bug_ID”列的“”。 'bug_id' 是一个 int,我有 32 列。

考虑到归档行数是可变的,我怎样才能告诉他忽略这些行?

感谢您的帮助。

i'm using sql command load data to insert data in a csv file to mysql database. the problem is that at the end of the file there's a few line like ",,,,,,,,,,,,,,,,,," (the csv file is a conversion of an excel file). so when sql get to those lines he send me : #1366 - Incorrect integer value: '' for column 'Bug_ID' at row 661.
the 'bug_id' is an int and i have 32 column.

how can i tell him to ignore those lines considering the number of filed lines is variable?

thanks for your help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

南冥有猫 2024-09-19 14:31:56

MySQL 在读取分隔文本文件时支持“LINES STARTING BY "xxxx"”。如果可以的话,要求您的特定 .CVS 文件的每个数据行都带有“前缀”,而非数据行则没有该前缀。这使您能够根据需要将注释放入 .CSV 中。

MySQL 文档: 在文件中加载数据

您可以:

步骤1 - (可选)导出数据:

SELECT * 
  INTO OUTFILE "myFile.csv"
  COLUMNS TERMINATED BY ','
  OPTIONALLY ENCLOSED BY '"'
  ESCAPED BY '\\'
  LINES STARTING BY 'DATA:'
  TERMINATED BY '\n'
FROM table

步骤 2 - 导入数据

LOAD DATA INFILE "myFile.csv"
INTO TABLE some_table
     COLUMNS TERMINATED BY ','
     OPTIONALLY ENCLOSED BY '"'
     ESCAPED BY '\\'
     LINES STARTING BY 'DATA:'

实际上,您可以将 .csv 文件修改为如下所示:

# Comment for humans
// Comment for humans
Comments for us humans.
DATA:1,3,4,5,6,'asdf','abcd'
DATA:4,5,6,7,8,'qwerty','zxcv'
DATA:9,8,7,6,5,'yuio','hjlk'
# Comments for humans
// Comments for humans
Comments for humans
DATA:13,15,64,78,54,'bla bla','foo bar'

只有带有“DATA:”前缀的行才会被构造解释/读取。

我使用这种技术为需要外部控制信息的 SQL 脚本创建了一个“配置”文件。但存在人为因素,需要能够轻松操作 .csv 文件并理解其内容。

——J·乔根森——

MySQL supports a 'LINES STARTING BY "xxxx" ' for when reading delimited text files. If you can, require your specific .CVS file to have each data line with a 'prefix' and non-data lines to not have that prefix. This gives you the benefit of being able to putting comments into a .CSV if desired.

MySQL Doc: Load Data InFile

You can:

step 1 - (optionally) export data:

SELECT * 
  INTO OUTFILE "myFile.csv"
  COLUMNS TERMINATED BY ','
  OPTIONALLY ENCLOSED BY '"'
  ESCAPED BY '\\'
  LINES STARTING BY 'DATA:'
  TERMINATED BY '\n'
FROM table

step 2 - import data

LOAD DATA INFILE "myFile.csv"
INTO TABLE some_table
     COLUMNS TERMINATED BY ','
     OPTIONALLY ENCLOSED BY '"'
     ESCAPED BY '\\'
     LINES STARTING BY 'DATA:'

Effectively you can modify the .csv file to look like this:

# Comment for humans
// Comment for humans
Comments for us humans.
DATA:1,3,4,5,6,'asdf','abcd'
DATA:4,5,6,7,8,'qwerty','zxcv'
DATA:9,8,7,6,5,'yuio','hjlk'
# Comments for humans
// Comments for humans
Comments for humans
DATA:13,15,64,78,54,'bla bla','foo bar'

Only the lines with 'DATA:' prefix will be interpreted/read by the construct.

I used this technique to create a 'config' file for a SQL script that needed external control information. But there was a human element that needed to be able to easily manipulate the .csv file and understand its contents.

-- J Jorgenson --

暖阳 2024-09-19 14:31:56

我修好了:
我刚刚在 csv 解析器中添加了一个条件

while ((line = is.readLine()) != null) {

  if (!line.equals(",,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,"))
      {
  Iterator e = csv.parse(line).iterator();
      ......
      }

}

i fixed it:
i just added a condition on the line in my csv parser

while ((line = is.readLine()) != null) {

  if (!line.equals(",,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,"))
      {
  Iterator e = csv.parse(line).iterator();
      ......
      }

}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文