[英]How to delete specific row from sql server using nodeJs
我使用nodeJs作為后端語言,使用sql server作為數據庫。 我可以從數據庫中刪除整個表行,但是不能刪除特定行。 這是我的代碼...
router.delete('/student/:id', function (req, res, next) {
var connection = new Connection(config);
connection.on('connect', function(err) {
request = new Request("DELETE FROM student where id=@id", function(err) {
if (err) { console.log(err);}
});
request.addParameter('id');
request.on('done', function(rowCount, more) { console.log(rowCount + ' rows returned'); });
connection.execSql(request);
res.setHeader('Content-Type', 'application/json');
});
});
這里的問題似乎是調用參數不完整的addParameter()
函數。 request.addParameter()
需要三個參數,即。 參數名稱,數據類型,值。 您僅提供了參數名稱。
嘗試更改request.addParameter('id');
像這樣:
var TYPES = require('tedious').TYPES; // Get the TYPES enum.
request.addParameter('id', TYPES.Int, parseInt(req.params.id)); // Assuming your id is an integer. Use proper Types enum, and conversion, if its not.
您也可以在Microsoft網站上參考以下示例: https : //docs.microsoft.com/zh-cn/sql/connect/node-js/step-3-proof-of-concept-connecting-to-sql-using -node-JS
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.