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.

RobinHood
  • 13
  • 5
  • 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 – Rohit Gupta Dec 22 '22 at 07:58
  • You could try to use [mysql shell connector](https://stackoverflow.com/a/35276157/1765658),,, with [How to parse CSV](https://stackoverflow.com/a/69514496/1765658) – F. Hauri - Give Up GitHub Feb 13 '23 at 18:02

1 Answers1

-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

Vipertecpro
  • 3,056
  • 2
  • 24
  • 43