如何在使用 LOAD DATA 命令时跳过 csv 文件的行?
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
MySQL 在读取分隔文本文件时支持“LINES STARTING BY "xxxx"”。如果可以的话,要求您的特定 .CVS 文件的每个数据行都带有“前缀”,而非数据行则没有该前缀。这使您能够根据需要将注释放入 .CSV 中。
MySQL 文档: 在文件中加载数据
您可以:
步骤1 - (可选)导出数据:
步骤 2 - 导入数据
实际上,您可以将 .csv 文件修改为如下所示:
只有带有“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:
step 2 - import data
Effectively you can modify the .csv file to look like this:
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 --
我修好了:
我刚刚在 csv 解析器中添加了一个条件
while ((line = is.readLine()) != null) {
}
i fixed it:
i just added a condition on the line in my csv parser
while ((line = is.readLine()) != null) {
}