English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

NodeJS Basic Tutorial

NodeJS Express.js

NodeJS Buffer&URL;

NodeJS MySql

NodeJS MongoDB

NodeJS File (FS)

Other NodeJS

Node.js MySQL INSERT INTO query

The Node.js MySQL INSERT INTO query is used to insert one or more records into a MySQL table.

Node.js MySQL insert

  • Node.js MySQL example, insert records into the table

  • Node.js MySQL example, insert multiple records into the table

  • Access the properties of the result object

Node.js MySQL example, insert records into the table

//Import the mysql module
var mysql = require('mysql'); 
 
// Create a connection variable with the required details
var con = mysql.createConnection({ 
  host: "localhost", // The IP address of the server running mysql
  user: "arjun", // The username for the mysql database
  password: "password", // The corresponding password
  database: "studentsDB" // Use the specified database
 }); 
 
// Establish a connection with the database.
con.connect(function(err) { 
  if (err) throw err; 
  // If the connection is successful
  con.query("INSERT INTO students (name,rollno,marks) values ('Anisha',12,95})", function(err, result, fields) { 
    // If any errors occur during the execution of the above query, throw an error
    if (err) throw err; 
    // If there are no errors, you will get the result
    console.log(result); 
  }); 
 });

Run the Node.js MySQL program above in the terminal.

InsertMulIntoExample.js-Example of inserting multiple records into a table

//Import the mysql module
var mysql = require('mysql'); 
 
// Create a connection variable with the required details
var con = mysql.createConnection({ 
  host: "localhost", // The IP address of the server running mysql
  user: "arjun", // The username for the mysql database
  password: "password", // The corresponding password
  database: "studentsDB" // Use the specified database
 }); 
 
// Establish a connection with the database.
con.connect(function(err) { 
  if (err) throw err; 
  // If the connection is successful
  var records = [ 
    ['Miley', 13, 85], 
    ['Jobin', 14, 87], 
    ['Amy', 15, 74] 
  ]; 
  con.query("INSERT INTO students (name,rollno,marks) VALUES ?", [records], function (err, result, fields) { 
    // If any errors occur during the execution of the above query, throw an error
    if (err) throw err; 
    // If there are no errors, you will get the result
    console.log(result); 
  }); 
 });

Run the Node.js MySQL program above in the terminal.

InsertMulIntoExample.js-Example of accessing the properties of the result object

// Import the mysql module
var mysql = require('mysql'); 
 
// Create a connection variable with the required details
var con = mysql.createConnection({ 
  host: "localhost", // The IP address of the server running mysql
  user: "arjun", // The username for the mysql database
  password: "password", // The corresponding password
  database: "studentsDB" // Use the specified database
 }); 
 
// Establish a connection with the database.
con.connect(function(err) { 
  if (err) throw err; 
  // If the connection is successful
  var records = [ 
    ['Jack', 16, 82], 
    ['Priya', 17, 88], 
    ['Amy', 15, 74] 
  ]; 
  con.query("INSERT INTO students (name,rollno,marks) VALUES ?", [records], function (err, result, fields) { 
    // If any errors occur during the execution of the above query, throw an error
    if (err) throw err; 
    // If there are no errors, you will get the result
    console.log(result); 
    console.log("Number of rows affected: ", + result.affectedRows); 
    console.log("Number of records affected with warning: ", + result.warningCount); 
    console.log("Message from MySQL Server: ", + result.message); 
  }); 
 });
arjun@arjun-VPCEH26EN:~/workspace/nodejs$ node InsertMulIntoExample.js 
OkPacket { 
  fieldCount: 0, 
  affectedRows: 3, 
  insertId: 0, 
  serverStatus: 2, 
  warningCount: 0, 
  message: ' &Records: 3  Duplicates: 0 Warnings: 0', 
  protocol41: true, 
  changedRows: 0 } 
Number of rows affected: 3
Number of records affected with warning: 0
Message from MySQL Server: &Records: 3 Duplicates: 0 Warnings: 0

Conclusion:

In this Node.js tutorial – Node.js MySQL – Node.js MySQL INSERT INTO query, we learned how to insert one or more records into a table and access the properties of the result object.