Lubos Rendek

The only solution is determination.

Insert Data to Mysql Table Using Bash Command Line

| Comments

To insert data from bash command line you will need username and password to your MySQL database. Consider a following table called “fruits” with auto increment “id”:

1
2
3
+----+-----------------------+-------------+
| id | fruit          | update_time |
+----+-----------------------+-------------+

Next, we have a file with some data:

1
2
3
4
$ cat /tmp/fruits
apple
banana
lemon

At this sate we are ready to use bash for loop to insert data:

1
for i in $( cat /tmp/fruits ); do echo "INSERT INTO fruits (fruit) values ($i);" | mysql -u root -p password  mydatabase; done

Comments