我正在做一个网上商店的项目。我正在尝试从sqlite数据库中获取基于参数值的排序结果。我正在尝试根据"select“值对产品进行排序。
在我的app.js
app.get('/sortMaleProducts', function(request, response){
var sortValues = request.query.sortValue;
if(sortValues == 'priceASC')
{
sortValues = ["man", "price", "ASC"];
}
else if(sortValues == 'priceDESC')
{
sortValues = ["man", "price", "DESC"];
}
db.sortMaleProducts(sortValues, function(error, clothes){
if(error){
console.log("Error: "+ error);
}
else{
console.log(clothes)
const model = {
clothes
}
response.render("man.hbs", model)
}
})
})在我的db.js
exports.sortMaleProducts = function(sortValues, callback){
const query = 'SELECT * FROM products WHERE gender = ? Order by ?, ?'
db.all(query, sortValues, function(error, clothes){
console.log(clothes);
callback(error, clothes);
})如果我硬编码查询,如下所示:
const query = 'SELECT * FROM products WHERE gender = 'man' Order by price ASC'然后它works....But,我想使用用户输入,以便我可以重用代码..
发布于 2019-09-24 04:57:21
如果要按列排序,则该列名称必须直接出现在查询中。您所做的工作是按照字符串'price'和'ASC'对结果进行排序,这两个字符串对于每一行都是相同的,因此对结果的任何顺序都进行了排序。
您也不能在查询的其他地方使用列名作为参数,比如在要返回的列中或在WHERE中。当通过将语句编译成sqlite的内部字节码来准备语句时,它们必须存在,这发生在任何参数绑定或查询执行之前。
https://stackoverflow.com/questions/58066727
复制相似问题