Matt
Matt

Reputation: 187

Take text from a file and insert to a mysql table using a script

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

Upvotes: 1

Views: 5192

Answers (2)

deed02392
deed02392

Reputation: 5022

What kind of script? If this is in bash, you could do something like:

mysql -u root -p password dbname << EOF INSERT INTO tblename (fieldname) VALUES ('cat /path/to/file.txt'); EOF

Upvotes: 0

Marc B
Marc B

Reputation: 360572

Not even close for either. You can't "redirect" a string into mysql.

Try

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

instead.

The second version has even less of a chance to work - you'd just be inserting the name of the file into the database, not the file's contents.

Upvotes: 3

Related Questions