What is .CSV file?
The .CSV (Comma Separated Values) files are plain text files that contains a list of data separated by comma(,). It is a format best used for tabular data, row, and columns, exactly like a spreadsheet, just the difference is that the file is in the form of plain text. The idea of a CSV file is to perform export and import complex data from one application to another.
Note: Here, we are using sample.csv to demonstrate our program and procedure. In sample.csv we have stored the user’s Name, Age, Email, and City.
Import CSV into MySQL using Node.js:
- Create a node.js project using “npm init” and save your .csv file in the same directory.
- Install two packages “mysql” and “csvtojson” using the following command:
npm i mysql csvtojson
csvtojson: Its a csv parser to convert csv to json or column arrays.
- Now just create a file like index.js and write down the following code:
Run index.js file using the following command:
Explanation of code:
- At the first two lines of code we Import mysql and csvtojson.
const csvtojson = require('csvtojson'); const mysql = require("mysql")
- Line 10 – 23 : We created a connection to our database.
hostname = "localhost", username = "root", // Username of Mysql local server password = "root", // Password of Mysql local server databsename = "csvtomysql" // Database we are connecting to
- Line 23 – 38: We have connected to our database “csvtomysql” and created table named “sample” with desired fields according to our sample.csv file.
- Line 42 – 64 : We fetched sample.csv located in current directory and converted all the data to JSON.
- At line 43 all data in sample.csv is converted to JSON and stored in variable “source”
- Then we loop through each each row and extracted Name, Email, Age and City value from that row.
- At line 53, we created a array of values in Name, Email, Age and City i.e. the column data of ith row.
- Then we inserted that data into table using query “INSERT INTO sample values(Name, Email, Age, City)”
- At line 62 we are showing the inserted data to console.
So, this way we can import any data form a .csv file to our MYSQL database.
- Nodejs | Automatic restart NodeJs server with nodemon
- What is the best way to include CSS file? Why use @import?
- How to import config.php file in a PHP script ?
- Nodejs – Connect Mysql with Node app
- Export JSON to CSV file in Angular
- How to convert an array to CSV file in PHP ?
- How to download a CSV file in PHP that is triggered through a URL ?
- How to Convert CSV to JSON file having Comma Separated values in Node.js ?
- How to fetch data from JSON file and display in HTML table using jQuery ?
- Node.js | MySQL-Create Table Using Sequelize
- How to add table row in a table using jQuery?
- How to remove table row from table using jQuery ?
- How create table without using <table> tag ?
- How to load data from JSON into a Bootstrap Table?
- Encrypting Data in NodeJS
- Difference between node.js require and ES6 import and export
- CSS | @import rule
- How to import Google Fonts in HTML ?
- SASS | @import
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.