我可以将一个数组传递给nodeJS中的mysql插件,就像这样..
var data = {userId: 3, name: "sample"}
db.query('insert into my_table SET ?', data, function(err, result){...}
是否有类似的方法将数组传递给where子句中的select查询...而不指定所有字段?
var data = {userId: 3, name: "sample"}
db.query('select * from my_table WHERE ?', data, function(err, result){...}
似乎不起作用..也没有使用SET名称代替......
答案 0 :(得分:0)
database.conn.config.defaultQueryFormat = function (query, values) {
if (!values) return query;
var updatedQuery = query.replace("?", function () {
var whereClause = "";
for(var index in values){
whereClause += mysql.escapeId(index) + " = " + db.escape(values[index]) + " and ";
}
whereClause = whereClause.substring(0, whereClause.length - 5);
return whereClause;
});
return updatedQuery;
};
这似乎有效..例如 var val = db.query('select * from my_table where?',data,function(err,result){ }