1

I'm using a bash script to pull data from online sources. Right now I just have it writing to a text file, but it would be better if the script could automatically put this data into mysql tables. How can this be done? Examples would be helpful.

2 Answers 2

3

Suppose you download a .csv file. which has header and have a database test in mysql.

Download the file first.

     wget http://domain.com/data.csv -O data.csv

Dump the data to mysql table tbl

     cat <<FINISH | mysql -uUSERNAME -pPASSWORD test
         LOAD DATA INFILE 'data.csv' INTO TABLE `tbl`
         FIELDS TERMINATED BY ',' ENCLOSED BY '"' 
         LINES TERMINATED BY '\r\n' 
         IGNORE 1 LINES;
     FINISH

Here USERNAME must have FILE privilege.

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

Comments

2

You can use bash like this:

#!/bin/bash

params="dbname -uuser -ppassswd"

echo "SELECT * FROM table;" | mysql $params
# or 
mysql $params <<DELIMITER
SELECT * FROM table;
DELIMITER

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.