我知道Node.js是异步的,但我不知道为什么我的两个查询没有执行,只有第一个查询。我只是尝试查询第一个查询是否正常,但即使第二个查询没有执行。
这是我的代码:
sql = "INSERT INTO TB_DISPOSITIVOS_VOZ (DS_CLIENTID, FK_ID_GRUPO) ";
sql = sql + " VALUES ('" + alexaClientId + "', " + idgrupo + ") ";
connection.query(sql, function(erro2, rows2, fields2) {
if (!erro2) {
console.log("Inserted alexa in the group: " + idgrupo + "with the clientId: " + alexaClientId);
} else {
console.log("Error - When inserting alexa in the group" + erro2);
context.fail("Error - When inserting alexa in the group" + erro2);
}
});
sql1 = "DELETE FROM TB_FILA_PAREAMENTO_OTP WHERE DS_CLIENTID = '" + alexaClientId + "'";
connection.query(sql1, function(erro3, rows3, fields3) {
if (!erro3) {
console.log("Query Delete TB_FILA_PAREAMENTO_OTP OK");
} else {
console.log("Error - Delete query does not works correctly..." + erro3);
context.fail("Error - Delete query does not works correctly..." + erro3);
}
});
答案 0 :(得分:0)
您可以使用Q github promise
var Q = require('Q');
function query1(){
var defered = Q.defer();
sql = "INSERT INTO TB_DISPOSITIVOS_VOZ (DS_CLIENTID, FK_ID_GRUPO) ";
sql = sql + " VALUES ('" + alexaClientId + "', " + idgrupo + ") ";
connection.query(sql, function(erro2, rows2, fields2) {
if (!erro2) {
console.log("Inserted alexa in the group: " + idgrupo + "with the clientId: " + alexaClientId);
} else {
console.log("Error - When inserting alexa in the group" + erro2);
context.fail("Error - When inserting alexa in the group" + erro2);
}
});
return defered.promise;
}
function query2(){
var defered = Q.defer();
sql1 = "DELETE FROM TB_FILA_PAREAMENTO_OTP WHERE DS_CLIENTID = '" + alexaClientId + "'";
connection.query(sql1, function(erro3, rows3, fields3) {
if (!erro3) {
console.log("Query Delete TB_FILA_PAREAMENTO_OTP OK");
} else {
console.log("Error - Delete query does not works correctly..." + erro3);
context.fail("Error - Delete query does not works correctly..." + erro3);
}
});
return defered.promise;
}
Q.all([query1(),query2()]).then(function(results){
console.log('all done');
});
答案 1 :(得分:0)
我解决了我的问题。
使用这样的多个语句:
标记连接中的多语句:
multipleStatements: true
sql = "INSERT INTO TB_1 (DS_CLIENTID, FK_ID_GRUPO) ";
sql = sql + " VALUES ('" + alexaClientId + "', " + idgrupo + "); ";
sql1 = "DELETE FROM TB_2 WHERE DS_CLIENTID = '" + alexaClientId + "';";
sql2 = "DELETE FROM TB_3 WHERE DS_CLIENTID = '" + alexaClientId + "';";
sql = sql2 + sql + sql1;
connection.query(sql,[1,2,3], function(erro2, rows2, fields2) {
if (!erro2) {
console.log("Inserted alexa in the group: " + idgrupo + "with the clientId: " + alexaClientId);
} else {
console.log("Error - When inserting alexa in the group" + erro2);
context.fail("Error - When inserting alexa in the group" + erro2);
}
});