导入 CSV
适用于 ✅ 开源版 ✅ 专业版 ✅ 企业版
下面的 CSV 数据表示两个作者记录,它们可能之前由 jOOQ 的导出功能导出,然后在 Microsoft Excel 或任何其他电子表格工具中修改
ID,AUTHOR_ID,TITLE <-- Note the CSV header. By default, the first line is ignored 1,1,1984 2,1,Animal Farm
以下示例显示了如何映射源表和目标表。
// Specify fields from the target table to be matched with fields from the source CSV by position.
// Positional matching is independent of the presence of a header row in the CSV content.
create.loadInto(BOOK)
.loadCSV(inputstream, encoding)
.fields(BOOK.ID, BOOK.AUTHOR_ID, BOOK.TITLE)
.execute();
// Use "null" field placeholders to ignore source columns by position.
create.loadInto(BOOK)
.loadCSV(inputstream, encoding)
.fields(BOOK.ID, null, BOOK.TITLE)
.execute();
// Match target fields with source fields by "corresponding" name.
// This assumes that CSV row 1 is a header row containing the source field names.
create.loadInto(BOOK)
.loadCSV(inputstream, encoding)
.fieldsCorresponding()
.execute();
CSV 特定选项
您可以传递以下标志之一来指定应如何解析 CSV 内容
create.loadInto(BOOK)
.loadCSV(inputstream, encoding)
.fields(BOOK.ID, BOOK.AUTHOR_ID, BOOK.TITLE)
// Ignore a certain number of header rows. By default, this is 1.
.ignoreRows(1)
// The quote character for use with string content containing quotes or separators. By default, this is "
.quote('"')
// The separator character that separates columns. By default, this is ,
.separator(',')
// The null string allows for distinguishing between empty strings and null. By default, there is no null string.
.nullString("{null}")
.execute();
反馈
您对此页面有任何反馈吗? 我们很乐意听取您的意见!