我有以下用于从我的数据库中提取信息的函数
exports.getSinglePrice= function(dbConnection, uid, tableVariable ,table, callback){
var myQuery = 'SELECT ' + tableVariable + ' FROM ' + table + ' WHERE uid= ' + uid;
var query = dbConnection.query(myQuery, function (err, row, result) {
//console.log(query.sql);
if (err) {
console.log("getSinglePrice error");
console.error(err);
return;
}
var price= row[0].tableVariable;
console.log("\n\nprice is : " , price);
callback(price);
return;
});
};
我有 3 个 tableVariable,我需要提取它们。我遇到的问题是以下行:
var price= row[0].tableVariable;
我不能使用 tableVariable,因为我没有得到任何信息,但必须给出变量的确切名称,例如在我的例子中:
var price= row[0].MoneyForPrivacy;
或
var price= row[0].MoneyForLocation;
或
var price= row[0].MoneyForEnvironment;
关于如何概括我的函数的任何想法。
最佳答案
答案是执行以下操作:
price= row[0][tableVariable];
当 tableVariable 是一个字符串时。
关于mysql - 从 NodeJS 中的 mysql 查询中提取信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35920344/