简体   繁体   中英

PDOException : SQLSTATE[HY000]: General error: 7890 Can't find file

I am trying to run the following project

https://github.com/saeedvaziry/laravel-vue-polling-app.git'

i am following the project readme.md but i am getting a PDOException when i run

php artisan migrate:refresh --seed

My Question what is causing this error and how can i resolve it

Here is the cli output

 PDOException  : SQLSTATE[HY000]: General error: 7890 Can't find file 'C:UsersuserDesktoplaravel-vue-polling-appstorageips.csv'.

  at C:\Users\user\Desktop\laravel-vue-polling-app\database\seeds\IpAddressesTableSeeder.php:18
    14|         $ipsPath = storage_path('ips.csv');
    15|         $pdo = \DB::connection()->getPdo();
    16|         $pdo->exec("
    17|                         LOAD DATA LOCAL
  > 18|                                 INFILE '" . $ipsPath . "'
    19|                         INTO TABLE
    20|                                 `ip_addresses`
    21|                         FIELDS TERMINATED BY ','
    22|                         ENCLOSED BY '\"'

  Exception trace:

  1   PDO::exec("
                        LOAD DATA LOCAL
                                INFILE 'C:\Users\user\Desktop\laravel-vue-polling-app\storage\ips.csv'
                        INTO TABLE
                                `ip_addresses`
                        FIELDS TERMINATED BY ','
                        ENCLOSED BY '"'
                        LINES TERMINATED BY '
'
                        IGNORE 0 LINES;
        ")
      C:\Users\user\Desktop\laravel-vue-polling-app\database\seeds\IpAddressesTableSeeder.php:18

  2   IpAddressesTableSeeder::run()
      C:\Users\user\Desktop\laravel-vue-polling-app\vendor\laravel\framework\src\Illuminate\Container\BoundMethod.php:29

and here is the seeder file

<?php

use Illuminate\Database\Seeder;

class IpAddressesTableSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        $ipsPath = storage_path('ips.csv');
        $pdo = \DB::connection()->getPdo();
        $pdo->exec("
            LOAD DATA LOCAL
                INFILE '" . $ipsPath . "'
            INTO TABLE
                `ip_addresses`
            FIELDS TERMINATED BY ','
            ENCLOSED BY '\"'
            LINES TERMINATED BY '\r\n'
            IGNORE 0 LINES;
        ");
    }
}

None of the files have been altered by me and is as is in the repo as far as i know

Windows uses \\ for the path separator, which is also the escape character in MySQL. When you pass C:\\Users\\user\\Desktop\\laravel-vue-polling-app\\storage\\ips.csv to MySQL, it thinks the single \\ is the escape character, not the path separator. To fix this, do

$ipsPath = addslashes($ipsPath);

before you use it in your query.

As you can see

PDOException : SQLSTATE[HY000]: General error: 7890 Can't find file ' C:UsersuserDesktoplaravel-vue-polling-appstorageips.csv '. no directory separators ?

did you try to use [ESCAPED BY 'char']

Your seeder file $ips should be like

$ipsPath = addslashes(storage_path('ips.csv'));
...
....

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM