Home  >  Article  >  Database  >  How to Batch Import Multiple CSV Files into a MySQL Database on MAMP?

How to Batch Import Multiple CSV Files into a MySQL Database on MAMP?

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2024-11-02 13:41:30753browse

How to Batch Import Multiple CSV Files into a MySQL Database on MAMP?

Batch import multiple CSV files into MySQL database

You need to import multiple CSV files into MySQL database and want to find one A way to batch import.

For users running a MAMP server on Mac OSX, there is the following method:

Use a shell script:

  1. Create a file, such as import.sh, and add Replace its content with the following script:
#!/usr/bin/env bash
cd yourdirectory
for f in *.csv
do
        mysql -e "USE yourDatabase LOAD DATA LOCAL INFILE '"$f"'INTO TABLE yourtable"
done
  1. Make sure the script has been given execution permission:
chmod +x import.sh
  1. Run the script in the terminal:
./import.sh

This script iterates through all the CSV files in your CSV folder and imports them into a table named yourtable using the MySQL LOAD DATA LOCAL INFILE statement. Replace yourdatabase with the name of the database you want to import data into, and yourtable with the name of the table you want to import data into.

The above is the detailed content of How to Batch Import Multiple CSV Files into a MySQL Database on MAMP?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn