An exception occurred while executing 'LOAD DATA LOCAL INFILE

元气小坏坏 提交于 2019-12-04 21:48:44

Got the same exception message. I was able to perform this by using PDO dirtectly.

I have the same problem... I solve it by replace:

LOAD DATA LOCAL INFILE ...

by

LOAD DATA INFILE

Local option: When this switch is specified the input file will be read from the host the MySQL client is running on instead of the MySQL server. When this switch is not specified, the input file will be read from the data directory on the MySQL host. To use LOAD DATA without the LOCAL switch, the connected user must have the "FILE" privilege.(http://geekology.co.za/article/2010/02/how-to-import-or-export-csv-or-text-files-to-from-mysql-databases)

I had tried to pass PDO::MYSQL_ATTR_LOCAL_INFILE parameter through doctrine configuration options parameter, but did not work, This is the solution finally working for me.

    $dbhost = $this->getContainer()->getParameter('database_host');
    $dbuser = $this->getContainer()->getParameter('database_user');
    $dbpass = $this->getContainer()->getParameter('database_password');
    $connParams = $em->getConnection()->getParams();

    $pdoConn = new \PDO('mysql:host=' . $dbhost . ';dbname=' . $connParams['dbname'], $dbuser, $dbpass, array(
        \PDO::MYSQL_ATTR_LOCAL_INFILE => true
    ));

    $sql = "SET FOREIGN_KEY_CHECKS=0";
    $stmt = $pdoConn->prepare($sql);
    $stmt->execute();

    $sql = "LOAD DATA LOCAL INFILE '$file' IGNORE INTO TABLE $tablename FIELDS TERMINATED BY '|' $columns";

    $stmt = $pdoConn->prepare($sql);
    $stmt->execute();
spiritoo

The two working answers are workaround :

  • The accepted answer proposes to use a PDO connection
  • Another answer proposes to remove the LOCAL keyword, but it's not always possible

A third, clean solution, is to enable the LOCAL option in Doctrine, but it is a bit tricky :

https://stackoverflow.com/a/24759583/425204

Note : this requires also that LOCAL is enabled in MySQL config on the server : my.cnf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!