0

I am trying to load data into mysql database using the following script

    #!/bin/bash

    DB_USER='my_user';
    DB_PASSWD='passwd';

    DB_NAME='db_name';
    TABLE='rass';

    INPUT_FILE='/full/path/to/re.txt';

    SQL="USE $DB_NAME; LOAD LOCAL DATA INFILE '$INPUT_FILE' REPLACE INTO TABLE `$TABLE` LINES TERMINATED BY '|' CHARACTER SET utf8;"
    mysql --user=$DB_USER --password=$DB_PASSWD --default_character_set utf8 $DB_NAME

When i run the command, it displays the mysql manual and none of the data in the file is inserted. Any ideas on how i can modify my code to insert the data

2

Use a here document.

mysql [args] << EOF
USE $DB_NAME; 
LOAD LOCAL DATA INFILE "$INPUT_FILE" REPLACE INTO TABLE "$TABLE" LINES TERMINATED BY '|' CHARACTER SET utf8;
QUIT;
EOF

(I suppose you don't want in fact single quotes around variables references).

Also, you might setup a ~/.my.cnf file so that non-interactive management is a bit simpler, for example:

[client]
user=root
password="some pass"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.