Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to import a csv file to mysql using node.js?

Right now I am trying with the library fast-csv doing this:

var stream = fs.createReadStream("./google.csv");
  csv
   .fromStream(stream, {headers : ["Name","E-mail 1 - Value"], ignoreEmpty: true})
   .on("data", function(data){
       console.log(data);
   })
   .on("end", function(){
       console.log("done");
   });

But it throws this error: "column header mismatch expected: 2 columns got: 57"

Do you know how can I avoid that? should I use a different library/approach

Another problem I am facing is that I get the result in hexadecimal... how can I parse it correctly?

like image 585
Ced Avatar asked Oct 29 '14 19:10

Ced


People also ask

How do I import a CSV file into MySQL?

In the Format list, select CSV. Changing format-specific options. If the csv file is delimited by a character other than a comma or if there are other specifications to the csv files, we can change it in this portion. Click Go to start importing the csv file and the data will be successfully imported into MySQL.


1 Answers

You can use node module 'csv-parse'.

  1. read the csv file using node 'fs' module
  2. pass the data to csv parser and you will get arry of array, where inner array represents each row.

Take a look at the following code.

var csvParser = require('csv-parse');

fs.readFile(filePath, {
  encoding: 'utf-8'
}, function(err, csvData) {
  if (err) {
    console.log(err);
  }

  csvParser(csvData, {
    delimiter: ','
  }, function(err, data) {
    if (err) {
      console.log(err);
    } else {
      console.log(data);
    }
  });
});

Here filePath is path of the csv file and the delimiter will be as per your file. It is the character that separates fields in csv file(can be ',', '.', etc).

like image 114
Asis Datta Avatar answered Sep 17 '22 21:09

Asis Datta