MySQL – Insert MySQL from a Linux terminal

Insert MySQL from a Linux terminal… here is a solution to the problem.

Insert MySQL from a Linux terminal

I have a question. I can insert a record into the mysql database from a linux terminal using the following command:

mysql dbTest insert into tablename values(1,"b","c")

Now I have a file in Linux with some records
For example:

$cat file

2, "c", "e"
3, "r", "q"
4, "t", "w"
5, "y", "e"
6, "u", "r"
7, "g", "u"
8, "f", "j"
9, "v", "k"

I don’t know how to insert all records in a file from a linux terminal into a mysql database.

I’m going to use a bash file, but I don’t know =(

Solution

Making a series of insertions is not the best choice in terms of performance. Since your input data exists in CSV format, you’d be better off executing a bulk load as suggested by @Kevin:

mysql dbTest -e "LOAD DATA INFILE './file' INTO TABLE tablename FIELDS TERMINATED BY ','"

Related Problems and Solutions