0

I'm uploading a csv file using the script

values ()
{
cat "$@" | \
while IFS=, read  -r a b c d;do 

printf ' %s\n' "$c"

done | \
paste -sd,-
}

Inserting data using

printf 'INSERT INTO `manufacturemap`(`manufacture_name`)VALUES(%s)\n'     "$(values $_csv_files)" | \
  mysql -u"$_db_user" -p"$_db_password" "$_db"

I get the following error

 ERROR 1054 (42S22) at line 1: Unknown column 'c' in 'field list'

I've been stuck here for the past few hours,Please help me.

Here is the Input(csv file):

    a,b,c,d
1.01100156278101E+15,2014/07/08,2014/07/08,"Cash Withdrawal by Cheque-173320--TT1421957901"
1.01100156278101E+15,2014/07/08,2014/07/08,"Cheque Paid-173261--TT1421951241"
1.01100156278101E+15,2014/07/08,2014/07/08,"Cheque Paid-173298--TT1421951226"
1.01100156278101E+15,2014/06/08,2014/06/08,"Cash Withdrawal by Cheque-173319--TT1421858465"
7
  • Output of your function with your csv file: 2014/07/08, 2014/07/08- 2014/07/08, 2014/06/08 Commented Jun 7, 2015 at 5:55
  • @Cyrus but I keep getting the error,why is that? Commented Jun 7, 2015 at 5:57
  • I don't know. Your question lacks the upload part of your script. Commented Jun 7, 2015 at 5:58
  • @anubhava expected output? well I need to convert the csv data into a table format so that I can put the values under the column c to database. Commented Jun 7, 2015 at 6:19
  • Yes that is what expected output will be. Can you show those insert sql that you expect from above input. Commented Jun 7, 2015 at 6:23

1 Answer 1

2

Better to use awk on your csv file like this to generate sql queries:

awk -F, 'NR>1{printf "INSERT INTO `manufacturemap`(`manufacture_name`) VALUES(\047%s\047)\n",
          $3}' file.csv
INSERT INTO `manufacturemap`(`manufacture_name`) VALUES ('2014/07/08')
INSERT INTO `manufacturemap`(`manufacture_name`) VALUES ('2014/07/08')
INSERT INTO `manufacturemap`(`manufacture_name`) VALUES ('2014/07/08')
INSERT INTO `manufacturemap`(`manufacture_name`) VALUES ('2014/06/08')

EDIT: To do that without awk you can do:

while IFS=, read -r _ _ c _; do
   [[ $((++i)) != "1" ]] && 
      printf 'INSERT INTO `manufacturemap`(`manufacture_name`) VALUES (\047%s\047)\n' "$c"
done < file.csv
INSERT INTO `manufacturemap`(`manufacture_name`) VALUES ('2014/07/08')
INSERT INTO `manufacturemap`(`manufacture_name`) VALUES ('2014/07/08')
INSERT INTO `manufacturemap`(`manufacture_name`) VALUES ('2014/07/08')
INSERT INTO `manufacturemap`(`manufacture_name`) VALUES ('2014/06/08')
Sign up to request clarification or add additional context in comments.

2 Comments

I know,but I'm supposed to do this with a shell script.
Yes,it does help,But its also inserting in the header name.How to exclude that?

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.