Node.js MySQL Insert Record

In this post, we will discuss how to insert rows/records in MySQL database in a XAMPP Server using Node.js Script.

It is important to install mysql package in node.js

Command to install the mysql package:

CopiedCopy Code
npm install mysql
				
Steps:

Now let's see steps

  1. First start your XAMPP Server (Both Apache and MySQL).
  2. Open Notepad or any text-editor and write the Node.js script
  3. In that script, first we have to load the mysql package using the below syntax
  4. var mysql_package = require('mysql');
  5. Create the connection using the server,username and password.
  6. CopiedCopy Code
    
    var connection_data = mysql_package.createConnection({
      host: "localhost",
      user: "root",
      password: "",
      database:"database_name"
    });
    
  7. Write the sql query to insert a record in a table
  8. Query method will take two parameters. The first parameter is the SQL Query and the second parameter will handle the result.

    If we want to insert more than one row at a time, then you need to pass three parameters. second parameter is list of rows variable

    For Single Row:

    CopiedCopy Code
    
    connection_data.connect(function(err) {
      var sql = "INSERT INTO table_name VALUES (value1,value2...........)";
      connection_data.query(sql, function (err, result) {
       console.log("Row inserted Successfully!");
      });
    });
    

    For Multiple Rows:

    CopiedCopy Code
    
    connection_data.connect(function(err) {
      var sql = "INSERT INTO table_name VALUES ?";
      var rows = [
        [value1,....],.....
        [value1,....],
       
      ];
       connection_data.query(sql, [rows], function (err, result) {
       
        console.log("Successfully Inserted");
      });
    });
    	
  9. Now type the following command in your command prompt to run the script.
  10. node file_name.js

INSERT record Example 1:-

Let's insert a record in a village table.

CopiedCopy Code

 // Load the mysql package
var mysql_package = require('mysql');

// Create the connection using the server,username and password.
//In my scenario - server is the localhost,
//username is root,
//password is empty.
//database is facility
var connection_data = mysql_package.createConnection({
  host: "localhost",
  user: "root",
  password: "",
  database:"facility"
});
connection_data.connect(function(err) {
 
 // Insert one record in village table.
  var sql = "INSERT INTO village VALUES ('Kakumanu', 'Guntur',110)";
  connection_data.query(sql, function (err, result) {
   console.log("Row inserted Successfully!");
  });
});
Output:
CopiedCopy Code

Row inserted Successfully!

So the table is:

alt=

It can also be possible to insert multiple records at a time.

INSERT record Example 2:-

Let's insert 4 records in a village table.

CopiedCopy Code

 // Load the mysql package
var mysql_package = require('mysql');
// Create the connection using the server,username and password.
//In my scenario - server is the localhost,
//username is root,
//password is empty.
//database is facility
var connection_data = mysql_package.createConnection({
  host: "localhost",
  user: "root",
  password: "",
  database:"facility"
});
connection_data.connect(function(err) {
//Insert 4 records into village table.
  var sql = "INSERT INTO village VALUES ?";
  var rows = [
    ['delhi', 'delhi',30],
    ['patna', 'patna',100],
    ['bapatla', 'guntur',40],
    ['gogulamudi', 'guntur',67]
  ];
   connection_data.query(sql, [rows], function (err, result) {   
    console.log("Successfully Inserted");
  });
});

Output:

CopiedCopy Code

Successfully Inserted

So the table is:

alt= Summary

So we seen how to insert record/s in XAMPP Server through Node.js script with two examples. make sure that you have to install mysql package and xampp server. It is important to pass the multiple rows list variable in query as second parameter.