在sequelize中删除单引号原始查询

时间:2015-08-20 08:51:44

标签: javascript mysql node.js sequelize.js

我正在尝试在sequelize中执行以下查询:

mysql> SELECT * FROM articles WHERE MATCH (title,body)
    -> AGAINST ('+MySQL -YourSQL' IN BOOLEAN MODE);

这是我的尝试:

var rawQuery = "SELECT * FROM users WHERE MATCH (email) AGAINST ('?*' IN BOOLEAN MODE)";

                return sequelize.query(rawQuery, {
                        replacements: [text],
                        type: sequelize.QueryTypes.SELECT
                    }).then(function(user) {
                        return user;
                });

问题是我收到错误,因为我传递的字符串包含单引号,这是我得到的错误:

Executing (default): SELECT * FROM users WHERE MATCH (email) AGAINST (''test'*' IN BOOLEAN MODE)
Unhandled rejection SequelizeDatabaseError: ER_PARSE_ERROR: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'test'*' IN BOOLEAN MODE)' at line 1

你知道修复它的方法是什么?

2 个答案:

答案 0 :(得分:1)

尝试转义引号:

var rawQuery = "SELECT * FROM users WHERE MATCH (email) AGAINST (\"?*\" IN BOOLEAN MODE)";

return sequelize.query(rawQuery, {
        replacements: [text],
        type: sequelize.QueryTypes.SELECT
    }).then(function(user) {
        return user;
});

答案 1 :(得分:0)

这个怎么样:

var rawQuery = 'SELECT * FROM users WHERE MATCH (email) AGAINST ("?*" IN BOOLEAN MODE)';