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

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

Linda Hamilton
Linda HamiltonOriginal
2024-11-01 16:05:30334browse

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

Seamlessly Importing Multiple CSV Files into MySQL Database

Importing data into a MySQL database from CSV files is a common requirement, but manual import can be tedious when dealing with a large number of files. This article explores an efficient batch import solution for importing multiple CSV files simultaneously into a MySQL database.

Solution for Batch CSV Import

To streamline the import process, execute the following shell script:

<code class="bash">#!/usr/bin/env bash

# Change to the directory containing your CSV files.
cd yourdirectory

# Iterate through all CSV files in the directory.
for f in *.csv
do
  # Import the current CSV file into the specified MySQL database and table.
  mysql -e "USE yourDatabase LOAD DATA LOCAL INFILE '"$f"' INTO TABLE yourtable"
done</code>

Customization and Usage

  • Database Details: Replace "yourDatabase" with the name of your target database in MySQL.
  • Table Name: Change "yourtable" to the name of the table you want to import data into.
  • File Location: Execute the script from the directory containing your CSV files.

Benefits of Batch Import

  • Automation: Automates the import process, saving time and effort.
  • Efficiency: Imports multiple CSV files simultaneously, significantly improving performance.
  • Scalability: Can be used to import large datasets efficiently.

By leveraging this batch import script, you can seamlessly import multiple CSV files into your MySQL database in a streamlined and time-saving manner.

The above is the detailed content of How to Efficiently Batch Import Multiple CSV Files into a MySQL Database?. 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