Node.js MySQL INSERT INTO Query is used to insert one or more records into MySQL Table.
Node.js MySQL INSERT INTO
- Node.js MySQL Example to INSERT a record INTO a table
- Node.js MySQL Example to INSERT multiple records INTO a table
- Accessing properties of Result Object
Node.js MySQL Example to INSERT a record INTO a table
// include mysql module var mysql = require('mysql'); // create a connection variable with the required details var con = mysql.createConnection({ host: "localhost", // ip address of server running mysql user: "arjun", // user name to your mysql database password: "password", // corresponding password database: "studentsDB" // use the specified database }); // make to connection to the database. con.connect(function(err) { if (err) throw err; // if connection is successful con.query("INSERT INTO students (name,rollno,marks) values ('Anisha',12,95)", function (err, result, fields) { // if any error while executing above query, throw error if (err) throw err; // if there is no error, you have the result console.log(result); }); }); |
Run above Node.js MySQL program in Terminal.
arjun@arjun-VPCEH26EN:~/workspace/nodejs$ node InsertIntoExample.js OkPacket { fieldCount: 0, affectedRows: 1, insertId: 0, serverStatus: 2, warningCount: 0, message: '', protocol41: true, changedRows: 0 } |
Node.js MySQL Example to INSERT multiple records INTO a table
// include mysql module var mysql = require('mysql'); // create a connection variable with the required details var con = mysql.createConnection({ host: "localhost", // ip address of server running mysql user: "arjun", // user name to your mysql database password: "password", // corresponding password database: "studentsDB" // use the specified database }); // make to connection to the database. con.connect(function(err) { if (err) throw err; // if 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 error while executing above query, throw error if (err) throw err; // if there is no error, you have the result console.log(result); }); }); |
Run above Node.js MySQL program in Terminal.
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 } |
Accessing properties of Result Object
Once the INSERT INTO Query is executed, MySQL Server responds with a result object. The properties of result object like affectedRows
, serverStatus
, changedRows
etc., could be accessed using DOT (.) operator.
// include mysql module var mysql = require('mysql'); // create a connection variable with the required details var con = mysql.createConnection({ host: "localhost", // ip address of server running mysql user: "arjun", // user name to your mysql database password: "password", // corresponding password database: "studentsDB" // use the specified database }); // make to connection to the database. con.connect(function(err) { if (err) throw err; // if 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 error while executing above query, throw error if (err) throw err; // if there is no error, you have 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 have learned to insert one or more records into a table and access properties of the result object.