RobinHood
RobinHood

Reputation: 13

Bash script to import the multiple CSV files into a mysql database using load data local infile command

  1. I have Multiple CSV files which are stored in one of the folder then I need to use these folder to fetch the csv files then load them into Database Table.

  2. This script need to prepare in Bash with parameterized fields like InputFolderPath(loop Csv Files), DatabaseConnection, SchemaName, TableName then pass these fields using

    Load Data Local Infile Command.

Upvotes: 0

Views: 610

Answers (1)

Vipertecpro
Vipertecpro

Reputation: 3284

This worked for me,

for f in /var/www/path_to_your_folder/*.csv
do
    mysql -e "use database_name" -e "
        load data local infile '"$f"' into table your_table_name FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"' ESCAPED BY '\"' LINES TERMINATED BY '\r\n' IGNORE 1 LINES (column_name1, @date_time_variable1, column_name3)
SET some_column_name_which_contains_date = STR_TO_DATE(@date_time_variable1, '%d-%m-%Y');" -u your_mysql_username_here --p --local-infile=1

    echo "Done: '"$f"' at $(date)"

done

This script will prompt password for mysql.

i am using this script on ec2 + ubuntu

Upvotes: -1

Related Questions