LOAD DATA INFILE用于导入CSV的用途?

时间:2013-10-24 12:28:12

标签: mysql ruby-on-rails ruby csv load-data-infile

在本地计算机上运行的常规LOAD DATA INFILE语法是:

LOAD DATA [LOW_PRIORITY | CONCURRENT] LOCAL INFILE 'file_name'
[REPLACE | IGNORE]
INTO TABLE tbl_name
[CHARACTER SET charset_name]
[{FIELDS | COLUMNS}
    [TERMINATED BY 'string']
    [[OPTIONALLY] ENCLOSED BY 'char']
    [ESCAPED BY 'char']
]
[LINES
    [STARTING BY 'string']
    [TERMINATED BY 'string']
]
[IGNORE number LINES]

我正在用Ruby编写一个程序,它应该能够将各种CSV导入到MySQL表中。

CSV文件完美地存储在变量中,并且会提取标题并完美地创建表格。问题是我获得的每个CSV文件都不同,并且必须修改LOAD DATA LOCAL INFILE参数才能识别CSV文件格式。

例如,在一个CSV中,LINES TERMINATED BY选项必须设置为'\n',而另一个选项必须设置为'\r'。同样,在一个CSV中,ESCAPED BY '[char]'必须存在才能正确导入,而另一个必须不存在。

有没有办法提供多个值来检查?与TERMINATED BY '\n or \r'ENCLOSED BY '\ or "'一样?

EDIT:

当我这样做时:

FasterCSV.foreach(csv) do |row|
  @first = row
  break
end

我得到了第一行。是否可以检测到行终止符,无论是该行的\n还是\r\n还是\r

1 个答案:

答案 0 :(得分:0)

我也有这个问题,所以我最终为一些" testLines"写了一个迷你解析器。在加载每个文件之前。

public static void findTerminator(File file) throws FileNotFoundException {
    BufferedReader lines = new BufferedReader(new FileReader(file));
    int countLines = 0;
    int testLines = 15;
    int c;
    int[] terminators = { 0x0A, 0x0D, 0x0D0A }; //\n, \r, \r\n
    int[] counters = { 0, 0, 0 };
    try {
        while (((c = lines.read()) != -1) && (countLines <= testLines)) {
            for (int d = 0; d < terminators.length; d++) {
                if (c == terminators[d]) { 
                    counters[d]++; 
                    countLines++;
                }
            }
        }
    } 
    catch (IOException e) { e.printStackTrace(); }

    int max = 0;
    int maxindex = 0;
    for (int i = 0; i < counters.length; i++) {
        if (max < counters[i]) { 
            max = counters[i]; 
            maxindex = i; 
        }
    }
    terminator = (char)terminators[maxindex];
    System.out.println("Terminator: '" + terminators[maxindex] + "'");
}