How to Import Multiple csv files into a MySQL Database

后端 未结 9 1616
深忆病人
深忆病人 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:46

    You could use a shell script to loop through the files (this one assumes they're in the current directory):

    #!/bin/bash
    
    for f in *.csv
    do
        mysql -e "load data infile '"$f"' into table my_table" -u username --password=your_password my_database
    done
    
    0 讨论(0)
  • 2020-11-30 06:49

    In python you can use d6tstack which makes this simple

    import d6tstack
    import glob
    
    c = d6tstack.combine_csv.CombinerCSV(glob.glob('*.csv'))
    c.to_mysql_combine('mysql+mysqlconnector://usr:pwd@localhost/db', 'tablename')
    

    It also deals with data schema changes, creates table and allows you to preprocess data.

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

    For windows User use this batch

    echo off
    setlocal enabledelayedexpansion
    FOR %%f IN ("*.csv") DO (
      set old=%%~dpnxf
      set new=!old:\=\\!
      mysql -e "load data local infile '"!new!"' IGNORE into table email_us.business  COLUMNS TERMINATED BY ','" -u root
      echo %%~nxf DONE
    )
    
    • email_us -> DB
    • business -> Table
    • IGNORE -> Ignore duplicate insert and on error keep continue
    • ~dpnxf -> d for drive letter, p for path to file, n for filename, x for extension and f is file variable

    Steps: - Put that batch file in directory where all multiple csv files exist and named it as something.bat - run cmd.exe as adminstrator and call that something.bat file and enjoy importing...

    0 讨论(0)
提交回复
热议问题