使用 mysql 计算 csv 文件中的条目数以确定进一步处理

count number of entries in a csv file with mysql to determine further handling

我收到不同客户的不同 csv 文件。此文件中的条目数从 1 行到超过 100,000 行不等。 (对于 3 到 40 列的不同操作)

现在,我做这样的检查

private function determineFurtherHandling($file)
{
    if (count($file) > 10000)
    {
        return $uploadMethod = 'LOAD_DATA_LOCAL_INFILE';
    }

    return $uploadMethod = 'STANDARD';
}

确定我的程序是否应该使用加载数据本地 infile 或标准插入查询将数据上传到我的数据库。我想避免使用 php 和

加载文件
$file = file('response.csv');

我想知道是否可以直接在 mysql 查询中进行此检查,但似乎找不到可靠的答案。所以我的问题是,有没有办法摆脱检查并让 mysql 使用 CASE WHEN 语句或其他任何方式来确定它?

不检查行数,检查文件大小。 100000 不需要是硬性限制...

为避免加载整个文件,您可以使用此功能

/**
  * @param $file string : the realtive/absolute path to file + the filename
  * @return integer : the number of lines in the file. NULL if error when opening the file
  *
  */
function numberOfLines ($file) {
    $line = 0;

    $handler = fopen ($file, 'r');
    if (!handler) return NULL;
    while (!feof ($handler)) $line++;
    fclose ($handler);
    return $line;
}