Hi,
I have a .csv file created by a script with data in a tabular format.
I need to insert all the value into mysql database which is running in a different machine.
what is the command to export the .csv file into database using shell script.
I would probably read each line in and convert it to an SQL entry and apply all those entries to the database. If you're looking for a single shell command for that, you'll probably have a hard time. What have you tried? You might try contacting a group that focuses on mysql as opposed to shell scripting.
If you have MySQL client installed in your machine, then you could use a nifty little tool called mysqlimport to import your csv file into a MySQL table.
$
$ # show the table "emp" in the MySQL database "test"
$ mysql -e "desc emp" -u test -ptest test
+-------+---------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------------+------+-----+---------+-------+
| empno | decimal(10,0) | YES | | NULL | |
| ename | varchar(20) | YES | | NULL | |
| sal | decimal(7,2) | YES | | NULL | |
+-------+---------------+------+-----+---------+-------+
$
$ # see if it's empty
$ mysql -e "select count(*) from emp" -u test -ptest test
+----------+
| count(*) |
+----------+
| 0 |
+----------+
$
$ # It is. Now show the contents of my csv file
$ cat /tmp/emp.dat
1000,SCOTT,7987.25
2000,JAMES,3489.00
3000,DAVID,5520.75
4000,PETER,6789.01
$
$ # Now use the sqlimport program to load data into the emp table
$ mysqlimport -u root -proot --fields-terminated-by="," test "/tmp/emp.dat"
test.emp: Records: 4 Deleted: 0 Skipped: 0 Warnings: 0
$
$ # Verify the successful load
$ mysql -e "select * from emp" -u test -ptest test
+-------+-------+---------+
| empno | ename | sal |
+-------+-------+---------+
| 1000 | SCOTT | 7987.25 |
| 2000 | JAMES | 3489.00 |
| 3000 | DAVID | 5520.75 |
| 4000 | PETER | 6789.01 |
+-------+-------+---------+
$
$