Nodejs-Mysql查询表名作为变量 [英] Nodejs-Mysql Query table name as a variable
本文介绍了Nodejs-Mysql查询表名作为变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何将表名作为变量传递.基本上我想让e函数使用表名作为参数,并在使用nodejs的情况下将对象插入mysql数据库中的记录 我的功能将类似于
How can i pass table name as variable. Basically i want to make e function in which i will take table name as a parameter and object insert record in mysql database in using nodejs My function will be like
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
But i am wondering that how to pass table name in this query which is parameter taken from function. I am asking about syntax? I am using nodejs-mysql
推荐答案
尝试一下:
exports.insertIntoDb = function(tableName,insertObj) {
connection.query('INSERT INTO ?? SET ?', [ tableName, insertObj ], ...)
};
此处记录: https://github.com/mysqljs/mysql#preparing-queries
这篇关于Nodejs-Mysql查询表名作为变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文