如何将表名作为变量传递。基本上我想制作e函数,我将使用nodejs将表名作为参数和对象插入mysql数据库中的记录 我的功能就像
exports.insertIntoDb = function(tableName,insertObj) {
connection.query('INSERT INTO administrator SET ?',insertObj, function(error, result, fields) {
if(error){
res.json({
status:false,
message:'There is some problem with query'
})
}
else{
res.json({
status : true,
data : result,
message: 'user registered successfully'
})
}
});
}
但是我想知道如何在这个查询中传递表名,这是从函数中获取的参数。我在询问语法?我正在使用nodejs-mysql
答案 0 :(得分:1)
试试这个:
exports.insertIntoDb = function(tableName,insertObj) {
connection.query('INSERT INTO ?? SET ?', [ tableName, insertObj ], ...)
};
答案 1 :(得分:0)
内部app.js:
app.put('/updateCell', async function(req, res) {
console.log("REST: PUT /updateCell");
let orderInfo = req.body;
let cellValue = orderInfo.cell;
let CustomerName = orderInfo.CustomerName;
let ColumnName = orderInfo.columnName;
connection.query("UPDATE vehicles SET ?? = ? WHERE order_CustomerName = ?", [columnName, cellValue, customerName],
function(err, result) {
if (err) throw err;
});
res.send();
});
示例:
//cellValue = "Fluffiest hat of them all";
//customerName = "Jenny Hopkins";
//columnName = "description";
因此,SQL查询将与:
UPDATE order SET description = "fluffiest hat of them all" WHERE order_CustomerName = "Jenny Hopkins";