Node.js MySQL Insert Records
Node.js MySQL Insert Records
The INSERT INTO statement in MySQL is used to add new records into a database table. When working with Node.js, you can use the mysql module to easily insert data into a MySQL database. Let’s look at how to insert single and multiple records using Node.js.
Data Science Tutorial:–Click Here
Insert a Single Record
Let’s insert a single record into the employees table.
Step 1:
Create a JavaScript file named insert.js inside the DBexample folder and add the following code:
var mysql = require('mysql');
var con = mysql.createConnection({
host: "localhost",
user: "root",
password: "12345",
database: "updategadh"
});
con.connect(function(err) {
if (err) throw err;
console.log("Connected!");
var sql = "INSERT INTO employees (id, name, age, city) VALUES ('1', 'Ajeet Kumar', '27', 'Allahabad')";
con.query(sql, function (err, result) {
if (err) throw err;
console.log("1 record inserted");
});
});
Introduction to Applied AI:–Click Here
Step 2:
Open the command terminal and run:
node insert.js
After successful execution, the terminal will display:
Connected!
1 record inserted
Step 3:
Verify the inserted record using the following SQL query:
Download New Real Time Projects :–Click here
SELECT * FROM employees;
Insert Multiple Records
You can also insert multiple records at once using the same approach.
Step 1:
Create another file named insertall.js in the DBexample folder and add this code:
var mysql = require('mysql');
var con = mysql.createConnection({
host: "localhost",
user: "root",
password: "12345",
database: "updategadh"
});
con.connect(function(err) {
if (err) throw err;
console.log("Connected!");
var sql = "INSERT INTO employees (id, name, age, city) VALUES ?";
var values = [
['2', 'Bharat Kumar', '25', 'Mumbai'],
['3', 'John Cena', '35', 'Las Vegas'],
['4', 'Ryan Cook', '15', 'CA']
];
con.query(sql, [values], function (err, result) {
if (err) throw err;
console.log("Number of records inserted: " + result.affectedRows);
});
});
Step 2:
SQL Tutorial :–Click Here
Run the script using:
node insertall.js
Expected output:
Connected!
Number of records inserted: 3
Step 3:
Check the inserted records using:
SELECT * FROM employees;
The Result Object
Complete Python Course with Advance topics:-Click Here
When you execute an INSERT query in Node.js, the result object returned by MySQL provides details about the operation.
For example, it includes the number of affected rows, insert ID (if applicable), and server status information.
A simplified result object looks like this:
{
"fieldCount": 0,
"affectedRows": 1,
"insertId": 1,
"serverStatus": 2,
"warningCount": 0,
"message": "",
"protocol41": true,
"changedRows": 0
}
This allows you to verify whether your insertion was successful and how many records were affected.
Machine Learning Tutorial:–Click Here
Complete Advance AI topics:-Â CLICK HERE
Deep Learning Tutorial:– Click Here
nodejs mysql insert, mysql insert query nodejs, insert multiple records in mysql using node.js fails, node.js mysql insert, node.js mysql insert data, node.js mysql insert statement, insert record into mysql database, how do i avoid insert duplicate records in node mysql?, node js mysql insert data, node js mysql insert into, node js mysql insert variables, insert form data into mysql using node.js, insert query in node js mysql, insert data in mysql node js, nodejs mysql select, nodejs mysql how to insert data dynamically in mysql using node js node js insert data into mysql how to insert data in mongodb using node js using mongoose node js mysql query return result mysql2 insert multiple rows how to insert data in database using javascript in php how to connect html form to mysql database using node js express js mysql node js mysql insert records w3schools node js mysql insert records example



Post Comment