【问题标题】:How to wrap values within % % placefolder in mysql module in node.js?如何在 node.js 的 mysql 模块中的 % % placefolder 中包装值?
【发布时间】:2013-06-09 12:28:06
【问题描述】:

有没有办法在 Node.js 的 mysql 模块中编写一些查询?

select * from table_name where name like "%value%";

在 Node.js 中,您可以使用 ? 占位符来将一些值放入 sql 查询中。但是,下面的代码不起作用。

connection.query('select * from table_name where name like "%?%"', value, function(err, rows){
    if(err) throw err;
    console.log(rows);
});

当我编写上面的代码时,我得到一个空结果,因为 mysql 将我的代码解释为%'value'%,包括多余的单引号。那么有什么方法可以将value 放入sql 查询中而不包含额外的引号?

谢谢。

【问题讨论】:

    标签: mysql node.js express


    【解决方案1】:

    占位符用于转义,而不是真正用于文本格式。

    你可以改用这个:

    connection.query('select * from table_name where name like ?', '%' + value + '%', ...)
    

    【讨论】:

      猜你喜欢
      • 2016-05-14
      • 2023-02-17
      • 2010-09-14
      • 2013-03-05
      • 1970-01-01
      • 1970-01-01
      • 2013-03-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多