Import Export CSV In NodeJS MySQL (Simple Examples)

Welcome to a quick tutorial on how to import and export CSV with PHP and NodeJS. Need to generate a CSV report? Want to export a list from the database? Or need to import CSV data into the database? Well, it really isn’t too difficult. Let us walk through some simple examples – Read on!

ⓘ I have included a zip file with all the source code at the start of this tutorial, so you don’t have to copy-paste everything… Or if you just want to dive straight in.

 

 

TABLE OF CONTENTS

 

DOWNLOAD & NOTES

Firstly, here is the download link to the example code as promised.

 

QUICK NOTES

  • Download and unzip into your project folder.
  • Create a dummy database and import 1a-users.sql.
  • Open the terminal, and install the required modules – npm install mysql csv.
  • Run 2-import.js for the import demo, 3-export.js for the export demo – Remember to change the database settings to your own.
If you spot a bug, feel free to comment below. I try to answer short questions too, but it is one person versus the entire world… If you need answers urgently, please check out my list of websites to get help with programming.

 

EXAMPLE CODE DOWNLOAD

Click here to download all the example source code, I have released it under the MIT license, so feel free to build on top of it or use it in your own project.

 

 

MYSQL IMPORT & EXPORT CSV

All right, let us now get into the examples of how to import a CSV into MySQL, and export MySQL data to CSV.

 

PART 1) DUMMY DATA

1A) DUMMY USER DATABASE

1-users.sql
CREATE TABLE `users` (
  `user_id` bigint(20) NOT NULL,
  `user_name` varchar(255) NOT NULL,
  `user_email` varchar(255) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

ALTER TABLE `users`
  ADD PRIMARY KEY (`user_id`),
  ADD UNIQUE KEY `user_email` (`user_email`),
  ADD KEY `user_name` (`user_name`);

ALTER TABLE `users`
  MODIFY `user_id` bigint(20) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=1;

This is the dummy user table that we will use as an example. Very simple, only has 3 fields:

  • user_id The primary key, auto-increment.
  • user_name User name.
  • user_email User email, unique field.

 

1B) DUMMY CSV

1B-users.csv
Job Doe,job@doe.com
Joe Doe,joe@doe.com
Joi Doe,joi@doe.com
Jon Doe,jon@doe.com
Joy Doe,joy@doe.com

Don’t think this needs an explanation… We will be importing these 5 entries into the above table.

 

 

PART 2) IMPORT CSV TO MYSQL

2-import.js
// (A) LOAD MODULES
// npm install mysql csv
// https://www.npmjs.com/package/mysql
// https://www.npmjs.com/package/csv-parse
const mysql = require("mysql"),
      fs = require("fs"),
      csv = require("csv-parse");
 
// (B) CONNECT TO DATABASE - CHANGE SETTINGS TO YOUR OWN!
const db = mysql.createConnection({
  host: "localhost",
  user: "root",
  password: "",
  database: "test"
});
 
// (C) IMPORT CSV
// (C1) READ & IMPORT ROW-BY-ROW
fs.createReadStream("1b-users.csv")
.pipe(csv.parse())
.on("data", row => db.query(
  "INSERT INTO `users` (`user_name`, `user_email`) VALUES (?, ?)",
  row, (err, results, fields) => {
    console.log(row);
    if (err) { console.error(err.message); }
    else { console.log("USER ID:" + results.insertId); }
  }
))
 
// (C2) CLOSE DATABASE CONNECTION
.on("end", () => {
  db.end();
  console.log("Import complete");
});

This may seem intimidating to some beginners, but keep calm and look carefully. It is as straightforward as can be:

  1. We load the required modules – mysql, fs, and csv-parse.
  2. Connect to the database, and remember to change the settings to your own.
  3. Read the CSV file line-by-line, and insert them into the database.

Yep, that’s all.

 

 

PART 3) EXPORT MYSQL TO CSV

3-export.js
// (A) LOAD MODULES
// npm install mysql csv
// https://www.npmjs.com/package/mysql
// https://www.npmjs.com/package/csv-stringify
const mysql = require("mysql"),
      fs = require("fs"),
      csv = require("csv-stringify");

// (B) CONNECT TO DATABASE - CHANGE SETTINGS TO YOUR OWN!
const db = mysql.createConnection({
  host: "localhost",
  user: "root",
  password: "",
  database: "test"
});

// (C) CREATE CSV FILE
const writer = fs.createWriteStream("export.csv");
 
// (D) EXPORT MYSQL
// (D1) WRITE ROW-BY-ROW
db.query("SELECT * FROM `users`")
.on("error", err => console.error(err))
.on("result", row => {
  db.pause();
  csv.stringify([[
    row["user_id"], row["user_name"], row["user_email"]
  ]], (err, output) => {
    console.log(row);
    writer.write(output);
    db.resume();
  });
})
  
// (D2) EXPORT COMPLETE
.on("end", () => {
  db.end(); writer.end();
  console.log("Export complete");
});

Not going to explain line-by-line again, this one is straightforward.

  1. Load the required modules, mysql, fs, and csv-stringify.
  2. Connect to the database, and change settings to your own.
  3. Create an empty CSV file.
  4. Select row-by-row from the database and write to the CSV. Close both the database connection and CSV file when done.

 

 

EXTRA BITS & LINKS

That’s all for the tutorial, and here is a small section on some extras and links that may be useful to you.

 

LINKS & REFERENCES

 

THE END

Thank you for reading, and we have come to the end. I hope that it has helped you to better understand, and if you want to share anything with this guide, please feel free to comment below. Good luck and happy coding!

Leave a Comment

Your email address will not be published. Required fields are marked *