- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Inserting Records into Table using Node
In this article, we will see how we can insert data into a table using NodeJS. Read the complete article to understand how we can save data into the database table.
Before proceeding, please check the following steps are already executed −
mkdir mysql-test
cd mysql-test
npm init -y
npm install mysql
The above steps are for installing the Node - mysql dependecy in the project folder.
Insert a Record into the Students Table
For adding the new records into the MySQL table, firstly create an app.js file
Now copy-paste the below snippet in the file
Run the code using the following command
>> node app.js
Example
// Checking the MySQL dependency in NPM var mysql = require('mysql'); // Creating a mysql connection var con = mysql.createConnection({ host: "localhost", user: "yourusername", password: "yourpassword", database: "mydb" }); con.connect(function(err) { if (err) throw err; console.log("DB Connected!"); var sql = "INSERT INTO students (name, address) VALUES ('John', 'Delhi')"; con.query(sql, function (err, result) { if (err) throw err; console.log("Successfully inserted 1 record."); }); });
Output
After Inserting the record, we will get the following output −
Successfully inserted 1 record.
Insert Multiple Records into the Students Table
For adding new records into the MySQL table, firstly create an app.js file
Now copy-paste the below snippet in the file
Run the code using the following command
>> node app.js
Example
// Checking the MySQL dependency in NPM var mysql = require('mysql'); // Creating a mysql connection var con = mysql.createConnection({ host: "localhost", user: "yourusername", password: "yourpassword", database: "mydb" }); con.connect(function(err) { if (err) throw err; console.log("DB Connected!"); var sql = "INSERT INTO students (name, address) VALUES ('Pete', 'Mumbai'), ('Amy', 'Hyderabad'), ('Hannah', 'Mumbai'), ('Mike', 'Delhi')"; con.query(sql, function (err, result) { if (err) throw err; console.log("Successfully inserted multiple records into the table."); }); });
Output
The above program will give the following output after insertion −
Successfully inserted multiple records into the table.
- Related Articles
- Inserting records into a MySQL table using PreparedStatement in Java?
- Inserting random numbers into a table in MySQL?
- Display an error while inserting duplicate records in a MySQL table
- Can we use INTERVAL keyword while inserting date records in a MySQL table?
- Querying Data from Table using Node
- Escaping quotes while inserting records in MongoDB?
- Format date while inserting records in MySQL
- Inserting a node in a Javascript AVL Tree
- Write an JDBC example for inserting value for Blob datatype into a table?
- Inserting data into a new column of an already existing table in MySQL?
- Java Program for Inserting a Node in a LinkedList
- Inserting an Element into Deaps
- Write an JDBC example for inserting value for Clob data type into a table?
- JavaScript Program for Inserting a Node in a Linked List
- Inserting Array list into HANA database
