我在 sails.js 上执行多个 sql 查询时遇到问题
我想从 sails lift
上的文件运行脚本。
我在 /config/bootstrap.js
中编写了自定义处理
module.exports.bootstrap = function(cb) {
fs.readFile('SQL\\StoredProcedures\\MyProcedure.sql', 'utf8', function (err,data) {
if (err) {
console.log(err);
}
console.log(data);
MyModel.query(data, function(err, records){
if(err){
console.log(err);
}
});
});
// It's very important to trigger this callback method when you are finished
// with the bootstrap! (otherwise your server will never lift, since it's waiting on the bootstrap)
cb();
};
问题是,.query()
函数内部不接受多个查询。我的意思是,它确实接受我的文件中的以下内容:
DROP PROCEDURE IF EXISTS `MyProcedure`;
但它不会接受,而在我的文件中我有:
DROP PROCEDURE IF EXISTS `MyProcedure`;
SELECT * FROM something;
有办法执行这个文件吗?
最佳答案
这可以通过设置 config/datastores.js
来完成,如下所示:
module.exports = {
default: {
multipleStatements: true
}
}
通过将其添加到您的配置中,您可以让 Sails 处理查询的解析和执行。
问题是,默认情况下,Node MySQL 驱动程序不允许同时运行多个查询。这是为了防止 SQL 注入(inject)。
有关更完整的解释,请参阅@sgress454的评论:https://github.com/balderdashy/sails/issues/4192
关于mysql - sails.js 在 mysql 中运行多个命令查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34801020/