How to Import Multiple csv files into a MySQL Database

后端 未结 9 1615
深忆病人
深忆病人 2020-11-30 06:14

Is there a way to import multiple csv files at the same time into a MySQL database? Some sort of batch import?

I\'m on Mac OSX running a MAMP server.

I have

相关标签:
9条回答
  • 2020-11-30 06:33

    Use a shell script like this:

    #!/usr/bin/env bash
    cd yourdirectory
    for f in *.csv
    do
            mysql -e "USE yourDatabase LOAD DATA LOCAL INFILE '"$f"'INTO TABLE yourtable"
    done
    
    0 讨论(0)
  • 2020-11-30 06:33

    i had the same task to do with a lot of CSV files and create one table by CSV, so here is my script that i use in local under XAMP.

    <?php
    ini_set('display_errors',1);
    echo '### Begin Importation<br>';
    
    $mysqli  =  new mysqli(
    "localhost",
    "root",
    "",
    "mydatabase",
    3306
    );
    
    if (mysqli_connect_errno()) {
        printf("Connect failed: %s\n", mysqli_connect_error());
        exit();
    }
    
    $files = glob('C:\\xampp\\mysql\\data\\mev2\\*.csv');
    
    foreach($files as $file){
    
        //clean names if needed
        $filename = explode('\\',$file);
        $filename2clean = str_replace('.csv','', $filename[5]);//because my file is under 5 folders on my PC
        $n = strtolower(str_replace('fileprefix_','', filename2clean));
    
        echo '<br>Create table <b>'.$n.'</b><hr>';
    
        $sql = "CREATE TABLE IF NOT EXISTS `mydatabase`.`".$n."` (`email` varchar(60), `lastname` varchar(60), `firstname` varchar(60), `country` varchar(19)) DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;";
    
        if (!($stmt = $mysqli->query($sql))) {
            echo "\nQuery execute failed: ERRNO: (" . $mysqli->errno . ") " . $mysqli->error;
        };
    
        echo '<br>Import data from <b>'.$n.'</b><hr>';
    
        $sql = "LOAD DATA INFILE '".basename($file)."' INTO TABLE `mydatabase`.`".$n."`  
            FIELDS TERMINATED BY ';'
            LINES TERMINATED BY '\r'  
            IGNORE 1 LINES";
    
        if (!($stmt = $mysqli->query($sql))) {
            echo "\nQuery execute failed: ERRNO: (" . $mysqli->errno . ") " . $mysqli->error;
        };
    
    }
    
    echo '### Import finished !<br>';
    
    0 讨论(0)
  • 2020-11-30 06:34

    There's a little PHP script for you:

    #!/usr/bin/php
    <?
    mysql_connect('localhost','root','root'); // MAMP defaults
    mysql_select_db('yourdatabase');
    $files = glob('*.csv');
    foreach($files as $file){
        mysql_query("LOAD DATA INFILE '".$file."' INTO TABLE yourtable");
    }
    

    See the MySQL Manual for LOAD DATA INFILE options which fit your documents.

    0 讨论(0)
  • 2020-11-30 06:34

    I've modified Tom's script to solve few issues that faced

    #!/bin/bash
    
    for f in *.csv
    do
        mysql -e "load data local infile '"$f"' into table myTable fields TERMINATED BY ',' LINES TERMINATED BY '\n'"  -u myUser--password=myPassword fmeter --local-infile
    done
    
    1. load data local infile instead of load data infile : [file to be loaded was local to mysql server]
    2. Added delimiter switches to match my data.
    3. --local-infile to enabled local data load mode on client.
    0 讨论(0)
  • 2020-11-30 06:44

    @hlosukwakha you want to use mysqlimport. this searches for a table named like the file. use mysqlimport -help to find the correct parameters, but they're basically identical to mysql

    0 讨论(0)
  • 2020-11-30 06:44

    Using following shell script:

    for file in /directory/*.csv
    do
    echo "Importing file $file"
           chown mysql $file
            mysql Fortinet -u user -p'password' <<EOF
    
    LOAD DATA LOCAL INFILE '$file'
    IGNORE
    INTO TABLE tablename
    FIELDS TERMINATED BY ','
    ENCLOSED BY '"'
    LINES TERMINATED BY '\n'
    IGNORE 1 LINES;
    EOF
    
    echo "Completed importing '"$file"' "
    
    done
    
    0 讨论(0)
提交回复
热议问题