删除节点js mysql查询中的斜杠 [英] Remove slashes in node js mysql query

查看:45
本文介绍了删除节点js mysql查询中的斜杠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码.

pool.getConnection(function (err, connection) {
        connection.query("delete from userFiles where type = 1 and 
                          typeId = " + taskId + " and fileName 
                          NOT IN ('?') ",[oldFileNames], function (err, rows) {

        });
    });

现在的问题是,node js 正在生成这样的查询,

Now the problem is , node js is generating query like this,

delete from table_name where type = 1 and typeId = 1 and 
fileName NOT IN (\'\'upload_149f2b78e5fd4096781bb5b8719b874f.png,upload_e8185e896cb0f8bb0b66d807cc60922c.png\'\')

我什至这样试过,

connection.query("delete from userFiles where type = 1 and typeId = " + taskId + " and
                    fileName NOT IN ('" + oldFileNames + "') ", 
                      function (err, rows) {

这个生成的查询是这样的,

This generated query like this,

delete from userFiles where type = 1 and typeId = 1 and 
fileName NOT IN (\'upload_149f2b78e5fd4096781bb5b8719b874f.png,
upload_e8185e896cb0f8bb0b66d807cc60922c.png\')

两者都导致查询的 mysql 语法错误(斜线被追加).

Both are causing mysql syntax error for the query(slashes are getting appended).

你能提出建议吗,我能做些什么来摆脱这个错误.

Can you suggest, what i can do to get rid of this error.

推荐答案

您不应该自己在 ? 占位符周围添加引号.删除它们.

You're not supposed to add the quotes yourself around the ? placeholder. Remove them.

您还应该传递一个数组,而不是一个字符串.假设它是一个干净的字符串,你可以使用 split.

You should also pass an array, not a string. Assuming it's a clean string, you can just use split.

connection.query(
      "delete from userFiles where type = 1 and typeId = " + taskId +
      " and fileName NOT IN (?) ", [oldFileNames.split(/,\s*/)],
      function (err, rows) {

这篇关于删除节点js mysql查询中的斜杠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆