0

i have csv file with three fields(length,height,width).i want to read these values line by line and update these values in database.how to read the file line by line and update these values in data base.

we can use "load In file" query to update entire file to db.but i want to update the modified values from csv to db.so how to read file line by line and update these values.?

1
  • 2
    The most obvious answer is don't do it in shell, do it in a language with a proper csv reading library. It'll be faster and less likely to break on interesting csv formats. Commented Jul 12, 2011 at 9:50

1 Answer 1

3

As Douglas said in a comment, using sh to parse CSV is a really non-scalable plan. If the input file is anything but trivial, it will not work. However, for trivial input, you just need to set IFS. Here's an example that reads and tries to put the data into a MySQL database (I didn't test the SQL...the syntax may be (probably is) off):

#!/bin/sh
IFS=,

while read one two three; do
    echo "insert into table( col1, col2, col3 ) "'
        '"values ('$one', '$two', '$three');"
done |
mysql db;
Sign up to request clarification or add additional context in comments.

1 Comment

For numeric values (length, width, height), you probably don't need to quote the values in the VALUES list. However, the original discussion also mentioned 'update', but then you need a way to identify the row to be updated too, which is a nuisance since it is not clear that the file contains that information.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.