English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
When executing a MySQL query in Node.js, an object named Result Object is returned to the callback function. The result object contains a result set or properties that provide information about the execution of the query on the MySQL Server.
The content of the result object depends on the SQL query performed on the MySQL Server. The table below describes the result object of the query, such as select, insert, update, and delete.
MySQL query | Result object |
SELECT FROM | Result set containing records |
INSERT INTO | Object containing the execution state |
UPDATE | Object containing the execution state |
DELETE FROM | Object containing the execution state |
With the help of the following examples, we will understand how to access the properties of the records in the result set and how to access the properties of the execution state.
MySQL SELECT FROM query – Access ResultSet
MySQL INSERT INTO query – Access the properties of the result object
MySQL UPDATE query-Access the properties of the result object
MySQL DELETE FROM query-Access the properties of the result object
We can use the dot (.) operator to access the records in the result set as an array and as properties of the record.
// Node.js MySQL result object example // Import the mysql module var mysql = require('mysql'); // Create a connection variable with the required details var con = mysql.createConnection({ host: "localhost", // IP address of the server running MySQL user: "arjun", // MySQL database username 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("SELECT * FROM students", function err, result, fields) { // If any error occurs during the execution of the above query, throw an error if (err) throw err; // If there is no error, you will get the result // Iterate over all rows in the result Object.keys(result).forEach(function(key) { var row = result[key]; console.log(row.name) }); }); });
Run the above program using the node in the terminal
Terminal Output
arjun@arjun-VPCEH26EN:~/workspace/nodejs$ node selectUseResultObject.js John Arjun Prasanth Adarsh Raja Sai Ross Monica Lee Bruce Sukumar
We can use the DOT (.) operator to access 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", // IP address of the server running MySQL user: "arjun", // MySQL database username 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 error occurs during the execution of the above query, throw an error if (err) throw err; // If there is no error, 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); }); });
Run the above program using the node in the terminal
Terminal Output
arjun@arjun-VPCEH26EN:~/workspace/nodejs$ node MultipleInsertExample.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
We can use the DOT (.) operator to access 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", // IP address of the server running MySQL user: "arjun", // MySQL database username 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("UPDATE students SET marks=84 WHERE marks=74", function (err, result, fields) { // If any error occurs during the execution of the above query, throw an error if (err) throw err; // If there is no error, 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); }); });
Run the above program using the node in the terminal
Terminal Output
arjun@arjun-VPCEH26EN:~/workspace/nodejs$ node UpdateRecordsFiltered.js OkPacket { fieldCount: 0, affectedRows: 3, insertId: 0, serverStatus: 34, warningCount: 0, message: '(Rows matched: 3 Changed: 3 Warnings: 0', protocol41: true, changedRows: 3 } Number of rows affected : 3 Number of records affected with warning : 0 Message from MySQL Server: (Rows matched: 3 Changed: 3 Warnings: 0
We can use the DOT (.) operator to access 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", // IP address of the server running MySQL user: "arjun", // MySQL database username password: "password", // The corresponding password database: "studentsDB" // Use the specified database }); // Connect to the database. con.connect(function(err) { if (err) throw err; // If the connection is successful con.query("DELETE FROM students WHERE rollno>10", function (err, result, fields) { // If any error occurs during the execution of the above query, throw an error if (err) throw err; // If there is no error, 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); }); });
Run the above program using the node in the terminal
Terminal Output
arjun@arjun-VPCEH26EN:~/workspace/nodejs$ node deleteRecordsFiltered.js OkPacket { fieldCount: 0, affectedRows: 6, insertId: 0, serverStatus: 34, warningCount: 0, message: '', protocol41: true, changedRows: 0 } Number of rows affected : 6 Number of records affected with warning : 0 Message from MySQL Server :
In this Node.js tutorial – Node.js MySQL – we learned how to access the records of the result set and accessed the properties of the result object containing information about the query execution through examples.