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.
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.
-
2This is not a free code-writing service. It's about help. Please show your table schema, sample data and your script and query so farRohit Gupta– Rohit Gupta2022-12-22 07:58:04 +00:00Commented Dec 22, 2022 at 7:58
-
You could try to use mysql shell connector,,, with How to parse CSVF. Hauri - Give Up GitHub– F. Hauri - Give Up GitHub2023-02-13 18:02:57 +00:00Commented Feb 13, 2023 at 18:02
Add a comment
|
1 Answer
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