automated csv import to mysql server

Solutions on MaxInterview for automated csv import to mysql server by the best coders in the world

showing results for - "automated csv import to mysql server"
Koby
05 Sep 2019
1#!/bin/bash
2IMPORTED_FILE_PATH=/path/to/your/imported/file.csv
3TABLENAME=target_table_name
4DATABASE=db_name
5TMP_FILENAME=/tmp/${TABLENAME}.cvs
6# do nothing if imported file does not exist
7[ -f "$IMPORTED_FILE_PATH" ] || exit 0
8# if temporary file exists, then it means previous import job is running. Also do nothing
9[ -f "$TMP_FILENAME" ] && exit 0
10# Move it to tmp and rename to target table name
11mv "$IMPORTED_FILE_PATH" "$TMP_FILENAME"
12mysqlimport --user=mysqlusername --password=mysqlpassword --host=mysqlhost --local $DATABASE $TMP_FILENAME
13rm -f "$TMP_FILENAME"
14