Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I would like to take data (some text) from a file and insert it into a mysql field, using a script.

I have tried:

mysql -u root -p password dbname << INSERT INTO tblename (fieldname) VALUES (LOAD_FILE('filename'));

and

mysql -u root -p password dbname << EOF INSERT INTO tblename (fieldname) VALUES ('$filename'); EOF
share|improve this question
    
I wrote it in PHP and then added php file.php to a script –  Matt Feb 1 '12 at 15:25

1 Answer 1

up vote 3 down vote accepted

Not strictly Ubuntu related, but you want LOAD DATA INFILE, the documentation of which can be found at:

http://dev.mysql.com/doc/refman/5.1/en/load-data.html

Probably something like..

echo "LOAD DATA INFILE 'filename' INTO TABLE tblename;" | mysql -u root -p password dbname

You could also use dbname.tblename and then drop dbname from the mysql command, they'll be equivalent.

share|improve this answer
    
Thank you for your Answer, I did not know you could do it that way. I have given you a one up, but it is still not taking the data from the file and putting it in the field, it just comes up with null in the field. Im using Ubuntu if that helps. Thank you. –  Matt Jan 30 '12 at 18:40
    
I guess you'd have to post your table schema and a sample of the file for us to work out what might be going wrong :) –  Caesium Jan 30 '12 at 19:05

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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