Skip to content

Instantly share code, notes, and snippets.

@code-boxx
Last active November 9, 2023 02:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save code-boxx/511400abe1f68aaef8f2bb0c1b0bc81e to your computer and use it in GitHub Desktop.
Save code-boxx/511400abe1f68aaef8f2bb0c1b0bc81e to your computer and use it in GitHub Desktop.
NodeJS Import CSV Into Database

NODEJS IMPORT CSV

https://code-boxx.com/nodejs-import-csv-database/

NOTES

  1. Run unpack.bat (Windows) unpack.sh (Linux/Mac). This will automatically:
    • Install the required modules npm i sqlite3 csv-parser
    • Create the dummy database - node 1b-create.js
    • Run the import demo - node 3-import.js

LICENSE

Copyright by Code Boxx

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

CREATE TABLE users (
id INTEGER,
name TEXT NOT NULL,
email TEXT NOT NULL,
PRIMARY KEY("id" AUTOINCREMENT)
);
// (A) REQUIRED MODULES
const fs = require("fs"),
sqlite = require("sqlite3");
// (B) CREATE DUMMY DATABASE
const db = new sqlite.Database("users.db", err => {
if (err) { console.log(err); }
else {
db.exec(fs.readFileSync("1a-users.sql", "utf8"));
db.close();
console.log("Database created");
}
});
Joa Doe joa@doe.com
Job Doe job@doe.com
Joe Doe joe@doe.com
Jog Doe jog@doe.com
Joi Doe joi@doe.com
Jon Doe jon@doe.com
Jor Doe jor@doe.com
Jos Doe jos@doe.com
Jou Doe jou@doe.com
Joy Doe joy@doe.com
// (A) REQUIRED MODULES
const sqlite = require("sqlite3"),
fs = require("fs"),
csv = require("csv-parser");
// (B) IMPORT CSV
// (B1) OPEN DATABASE & CSV FILE
const db = new sqlite.Database("users.db");
fs.createReadStream("2-users.csv")
.pipe(csv({ headers : false }))
// (B2) INSERT ROW BY ROW
.on("data", row => {
row = Object.values(row);
console.log(row);
db.run(`INSERT INTO users (name, email) VALUES (?, ?)`, row);
})
// (B3) END
.on("end", () => {
console.log("Done!");
db.close();
});
call npm i sqlite3 csv-parser
node 1b-create.js
node 3-import.js
source "npm i sqlite3 csv-parser"
node ./1b-create.js
node ./3-import.js
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment