在Node.js中執(zhí)行MySQL查詢時,名為Result Object的對象將返回到回調(diào)函數(shù)。結果對象包含提供有關MySQL Server中查詢執(zhí)行信息的結果集或屬性。
結果對象的內(nèi)容取決于對MySQL Server進行的SQL查詢。下表內(nèi)容描述了查詢的結果對象,例如選擇,插入,更新和刪除。
MySQL 查詢 | 結果對象 |
SELECT FROM | 包含記錄的結果集 |
INSERT INTO | 包含執(zhí)行狀態(tài)的對象 |
UPDATE | 包含執(zhí)行狀態(tài)的對象 |
DELETE FROM | 包含執(zhí)行狀態(tài)的對象 |
我們將在以下示例的幫助下了解如何訪問結果集中記錄的屬性以及如何訪問執(zhí)行狀態(tài)的屬性。
MySQL SELECT FROM 查詢– 訪問ResultSet
MySQL INSERT INTO 查詢–訪問結果對象的屬性
MySQL UPDATE 查詢-訪問結果對象的屬性
MySQL DELETE FROM 查詢-訪問結果對象的屬性
我們可以使用點(.)運算符將結果集中的記錄作為數(shù)組和記錄的屬性來訪問。
// Node.js MySQL結果對象示例 // 引入mysql模塊 var mysql = require('mysql'); // 創(chuàng)建具有所需詳細信息的連接變量 var con = mysql.createConnection({ host: "localhost", // 運行mysql的服務器的IP地址 user: "arjun", // mysql數(shù)據(jù)庫的用戶名 password: "password", // 對應的密碼 database: "studentsDB" // 使用指定的數(shù)據(jù)庫 }); // 建立與數(shù)據(jù)庫的連接。 con.connect(function(err) { if (err) throw err; // 如果連接成功 con.query("SELECT * FROM students", function (err, result, fields) { // 如果在執(zhí)行上述查詢時出現(xiàn)任何錯誤,則拋出錯誤 if (err) throw err; // 如果沒有錯誤,您將得到結果 // 對結果中的所有行進行迭代 Object.keys(result).forEach(function(key) { var row = result[key]; console.log(row.name) }); }); });
使用終端中的節(jié)點運行以上程序
終端輸出
arjun@arjun-VPCEH26EN:~/workspace/nodejs$ node selectUseResultObject.js John Arjun Prasanth Adarsh Raja Sai Ross Monica Lee Bruce Sukumar
我們可以使用DOT(.)運算符訪問結果對象的屬性。
// 引入mysql模塊 var mysql = require('mysql'); // 創(chuàng)建具有所需詳細信息的連接變量 var con = mysql.createConnection({ host: "localhost", // 運行mysql的服務器的IP地址 user: "arjun", // mysql數(shù)據(jù)庫的用戶名 password: "password", // 對應的密碼 database: "studentsDB" // 使用指定的數(shù)據(jù)庫 }); // 建立與數(shù)據(jù)庫的連接。 con.connect(function(err) { if (err) throw err; // 如果連接成功 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) { // 如果在執(zhí)行上述查詢時出現(xiàn)任何錯誤,則拋出錯誤 if (err) throw err; // 如果沒有錯誤,您將得到結果 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); }); });
使用終端中的節(jié)點運行以上程序
終端輸出
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
我們可以使用DOT(.)運算符訪問結果對象的屬性。
// 引入mysql模塊 var mysql = require('mysql'); // 創(chuàng)建具有所需詳細信息的連接變量 var con = mysql.createConnection({ host: "localhost", // 運行mysql的服務器的IP地址 user: "arjun", // mysql數(shù)據(jù)庫的用戶名 password: "password", // 對應的密碼 database: "studentsDB" // 使用指定的數(shù)據(jù)庫 }); // 建立與數(shù)據(jù)庫的連接。 con.connect(function(err) { if (err) throw err; // 如果連接成功 con.query("UPDATE students SET marks=84 WHERE marks=74", function (err, result, fields) { // 如果在執(zhí)行上述查詢時出現(xiàn)任何錯誤,則拋出錯誤 if (err) throw err; // 如果沒有錯誤,您將得到結果 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); }); });
使用終端中的節(jié)點運行以上程序
終端輸出
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
我們可以使用DOT(.)運算符訪問結果對象的屬性。
// 引入mysql模塊 var mysql = require('mysql'); // 創(chuàng)建具有所需詳細信息的連接變量 var con = mysql.createConnection({ host: "localhost", // 運行mysql的服務器的IP地址 user: "arjun", // mysql數(shù)據(jù)庫的用戶名 password: "password", // 對應的密碼 database: "studentsDB" // 使用指定的數(shù)據(jù)庫 }); // 連接到數(shù)據(jù)庫。 con.connect(function(err) { if (err) throw err; // 如果連接成功 con.query("DELETE FROM students WHERE rollno>10", function (err, result, fields) { // 如果在執(zhí)行上述查詢時出現(xiàn)任何錯誤,則拋出錯誤 if (err) throw err; // 如果沒有錯誤,您將得到結果 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); }); });
使用終端中的節(jié)點運行以上程序
終端輸出
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 :
在本Node.js教程– Node.js MySQL –結果對象中,我們學習了訪問結果集的記錄,并通過示例訪問了包含有關查詢執(zhí)行信息的結果對象的屬性。