[英]How do I create a prepared statement in Node.JS for MSSQL?
我需要將在Javascript中定義的字符串插入到MSSQL表中。
這是我到目前為止的內容:
Javascript:
var message = "It's a great day today!";
$.post('www.server.com/message='+message, function(response){
console.log(response);
});
Node.js服務器:
//..... a bunch of code has been used to accept the HTTP request and get the message...
// for the purpose of this example, the message is asigned to 'NodeMsg'
var mssqldb = require("../core/mssql");
var NodeMsg = theMessageReceivedFromHTTPRequest;
function saveMessage(message) {
mssqldb.executeMssql("insert into messages (message, status) VALUES('"+message+"', 'new')", function (err) {
if (err) {
httpMsgs.show500(req, resp, err);
}//end if
else {
httpMsgs.sendJson(req, resp, 'success');
}//end else
});
};
mssql.js(node.js文件):
var mssqldb = require("mssql");
var settings = require("../settings");
exports.executeMssql = function(sql, callback) {
var conn = new mssqldb.Connection(settings.mssqlConfig);
conn.connect()
.then(function(){
var req = new mssqldb.Request(conn);
req.query(sql)
.then(function (recordset) {
callback(recordset);
})
.catch(function(err){
console.log(err);
callback(null, err);
});
})
.catch(function(err){
console.log(err);
callback(null, err);
});
};//end executeMssql
正在進行的摘要:
問題:在學習了更多有關面向對象程序的知識之后,我意識到執行插入的方式受到了廣泛的歡迎,因為它可以進行SQL注入。 另外,由於字符串中的單引號,所以代碼將在SQL查詢執行期間中斷。
解決方案:根據我發現的大多數資源,解決方案是使用“ Prepared Statements”。
我的問題:
我到底該如何轉換已完成的工作以利用准備好的語句? 我在網上搜索了HOURS,但找不到一個很好的示例來說明如何使用Node.JS for MSSQL(不是MySQL)編寫准備好的語句,這對於Node.Js的初學者來說是可以理解的。
如果您使用的是Tedious跨平台MSSQL驅動程序實現,則文檔位於此處: http ://tediousjs.github.io/tedious/parameters.html
基本上,您為需要注入的值准備一個帶有'@xxx'占位符的SQL語句,然后將這些參數的實際值綁定到您的請求,然后執行您的請求。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.