Node.js MySQL Insert Record

Websolutionstuff | Sep-29-2021 | Categories : MySQL Node.js

In this tutorial we will see how to insert data into MySQL table using Node.js. In previous node .js article I will give you example about how to connect MySQL database with Node.js and how to create MySQL database in Node.js.

So, now I will give you information about insert rows into mysql database table from node.js or insert multiple records into mysql using node.js. Before perform this article please make sure create database with connection. So, read below tutorial for connect database.

Example :

var mysql = require('mysql');

var con = mysql.createConnection({
  host: "localhost",
  user: "your_username",
  password: "your_password",
  database: "websolutionstuff"
});

con.connect(function(err) {
  if (err) throw err;
  console.log("Connected..!!");
  var sql = "INSERT INTO users (name, email) VALUES ('websolutionstuff', '[email protected]')";
  con.query(sql, function (err, result) {
    if (err) throw err;
    console.log("Record Inserted Successfully!");
  });
});

Run db_record_demo.js file

node db_record_demo.js

The response with console messages like

Connected..!!
Record Inserted Successfully!

 

 

Insert Multiple Records

Insert more than one record, make an array containing the values, and insert a question mark in the sql, which will be replaced by the value array :

INSERT INTO users (name, email) VALUES ?

Use query() method with SQL statement and data array like below :

connection.query(query, [data]);

Example :

var mysql = require('mysql');

var con = mysql.createConnection({
  host: "localhost",
  user: "your_username",
  password: "your_password",
  database: "websolutionstuff"
});

con.connect(function(err) {
  if (err) throw err;
  console.log("Connected..!");
  var sql = "INSERT INTO users (name, email) VALUES ?";
  var values = [
    ['websolutionstuff', '[email protected]'],
    ['dell', '[email protected]'],
    ['laravel', '[email protected]']
  ];
  con.query(sql, [values], function (err, result) {
    if (err) throw err;
    console.log("Number of records inserted: " + result.affectedRows);
  });
});

Run db_record_demo.js file

node db_record_demo.js

The response with console messages like :

Connected..!!
Number of records inserted: 3

 


You might also like :

Read Also : Laravel 8 Highcharts Example Tutorial

Read Also : Node.js Express CRUD Example with MySQL

Read Also : How To Import CSV File In MySQL Using Node.js

Read Also : How To Check Email Already Exist Or Not In Laravel

Recommended Post
Featured Post
Laravel 9 whereIn / whereNotIn / orWhereIn / orWhereNotIn
Laravel 9 whereIn / whereNotIn...

In this article, we will see Laravel 9 whereIn / whereNotIn / orWhereIn / orWhereNotIn Query Example. The whereIn()...

Read More

Oct-17-2022

How To Disable Future Date In jQuery Datepicker
How To Disable Future Date In...

In this tutorial, we will see how to disable future dates in jquery datepicker. In the date picker, today's dat...

Read More

Jun-17-2022

How To Get Multiple Checkbox Value In Javascript
How To Get Multiple Checkbox V...

Checkboxes let us make multiple choices. But how do you actually use them in JavaScript? It might seem tricky, especiall...

Read More

Jan-10-2023

How To Get The ID Of An Element Using jQuery
How To Get The ID Of An Elemen...

In this article, we will see how to get the id of an element using jquery. Using the jquery attr() method to get or...

Read More

Jul-04-2022