Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Insert into mysql from Bash script [duplicate]

Tags:

bash

mysql

Utilized a few posts here and Google and friends and I'm trying desperately to get something simple to work. I want to insert data into mysql via a bash script.

#!/bin/bash -x
DB_USER='jhatter';
DB_PASSWD='XXXXXXX';

DB_NAME='ppr';
TABLE='graph';

#Collect Rank Information from flightaware.com
day=$(date +"%m/%d/%y")
time=$(date +"%H:%M:%S")
rank=$(curl -s http://flightaware.com/adsb/stats/user/jdhatter11 | grep -i site-ranking-29371 | grep window | awk '{print $2}' | sed s/,//)

#mysql commands
mysql --user=$DB_USER --password=$DB_PASSWD $DB_NAME
INSERT INTO $TABLE (`id `, `day`, `time`, `rank`) VALUES (NULL, "$day", "$time", "$rank");
QUIT;

This script when ran manually stops abrubtly after mysql login. It actually brings the login up on screen and I have been inserted in the database. I'm a huge novice and please go easy if it's blatant, but why doesn't the script proceed to process the INSERT and instead throw me into mysql. I envision this all happening in the background...

like image 788
Joshua Hatter Avatar asked Aug 29 '16 20:08

Joshua Hatter


People also ask

How do I stop inserting duplicate records in MySQL?

Note − Use the INSERT IGNORE command rather than the INSERT command. If a record doesn't duplicate an existing record, then MySQL inserts it as usual. If the record is a duplicate, then the IGNORE keyword tells MySQL to discard it silently without generating an error.

How do you insert duplicates in SQL?

INSERT INTO table_name(c1) VALUES(c1) ON DUPLICATE KEY UPDATE c1 = VALUES(c1) + 1; The statement above sets the value of the c1 to its current value specified by the expression VALUES(c1) plus 1 if there is a duplicate in UNIQUE index or PRIMARY KEY .

How do you recover from the scenario of inserting duplicate values into a primary key column?

Solution 1You cannot insert duplicate values in a primary key column. Primary key columns are unique.

How do I connect to a bash script using MySQL?

Answer: It is simple to use any shell script to access a database, and using a shell script to select MySQL table rows is as simple as executing the /usr/bin/mysql executable and passing he SQL query as aeguments to MySQL. Below is a sample bash shell script accesses a MySQL database.


1 Answers

You can pass the commands in a here-document, like this:

mysql --user=$DB_USER --password=$DB_PASSWD $DB_NAME << EOF
INSERT INTO $TABLE (\`id\`, \`day\`, \`time\`, \`rank\`) VALUES (NULL, "$day", "$time", "$rank");
EOF

Notice that the ` need to be escaped. I also removed the QUIT command, as it's unnecessary (good tip @Ven, thanks).

Actually, since those column names don't contain special symbols, you don't actually need to quote them, and write the INSERT query a bit simpler, like this:

mysql --user=$DB_USER --password=$DB_PASSWD $DB_NAME << EOF
INSERT INTO $TABLE (id, day, time, rank) VALUES (NULL, "$day", "$time", "$rank");
EOF
like image 129
janos Avatar answered Oct 03 '22 23:10

janos