0
  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.

2
  • 2
    This is not a free code-writing service. It's about help. Please show your table schema, sample data and your script and query so far Commented Dec 22, 2022 at 7:58
  • You could try to use mysql shell connector,,, with How to parse CSV Commented Feb 13, 2023 at 18:02

1 Answer 1

-1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.