-3

enter image description here

I have csv file as attached here.

I have created one table using query

CREATE TABLE testdb.employee (
  emp_id INT(11) auto_increment NOT NULL,
  full_name varchar(100) NOT NULL,
  phone_no varchar(100) NOT NULL,
  email varchar(200) NOT NULL,
  hire_date DATE NOT NULL,
  salary DOUBLE NOT NULL,
  designation varchar(100) NOT NULL,
  PRIMARY KEY (emp_id)
);

I want to insert data of csv to that mysql table. Can someone help me to write Unix shell script?

Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175
DeepS
  • 1
  • 2
    Welcome! Please don't just ask us to solve the problem for you. Show us how you tried to solve the problem yourself, then show us exactly what the result was, and tell us why you feel it didn't work. Give us a clear explanation of what isn't working and provide a [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve). Read [How to Ask a good question](https://stackoverflow.com/help/how-to-ask). Be sure to take the tour and read this. – jasie Apr 11 '22 at 14:28
  • Does this answer your question? [How do I import CSV file into a MySQL table?](https://stackoverflow.com/questions/3635166/how-do-i-import-csv-file-into-a-mysql-table) – Romeo Ninov Apr 12 '22 at 11:27

1 Answers1

0

Step 1: Read the header

You will need to read the very first row and convert it into comma-separated values. If your csv is comma-separated already, then you just need to read the first line:

header=$(head -n 1 filename)

If it's separated with a different character, then you can explode it like

IFS=';' read -ra FIELDS <<< "$header"
for i in "${FIELDS[@]}"; do
  # process "$i"
done

where the separator is ;. If you have another separator, change accordingly. Now, if you had to explode it, then you can implode it like

IFS=, ; echo "${FIELDS[*]}"

and you can use the above as a function.

Step 2: Read each row

You will need to read all lines starting from the second in a loop until you reach the end of file (see Step 1 and https://www.shell-tips.com/bash/how-to-parse-csv-file/#gsc.tab=0)

Step 3: Parse your rows

You will need to make sure that you properly evaluate all cells, so if they are empty, you replace their value either with null or '', depending on which fits your need better. You also need to escape all apostrophe to avoid code breaks and SQL injections and wrap apostrophes around all textual data. This is not as simple as it sounds.

Step 4: Build your query

Your query will need to look like

insert into yourtable(column1, column2, column3) values
('a', 'b', 'c'),
('d', 'e', 'f');

Your header should already look good, but you will need to prepend insert into yourtable and wrap () around your header. Also, you will need to separate your rows with , and wrap () around all your rows, ending the query with ;.

Step 5: Execute

yourpassword | mysql -h yourhost -p -e"${yourquery}"

However, having a password in your script makes your script a very delicate file and you need to protect it by all means necessary.

Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175