我正在使用节点js批量上传到mysql数据库。我正在插入多行,如果执行中发生任何错误,我必须更新表中的列。我该怎么做。
var connection = createmysqlconnection();
var query1 = "INSERT INTO abc ("1","2");";
var query2 = "INSERT INTO xyz("3","4");";
var query3 = "INSERT INTO aaa("5","6");";
var sql = query1+query2+query3;
connection.query(sql, function (error, results, fields) {
if (error) {
console.log("Error occured");
connection.destroy();
throw error;
} else {
// connected!
console.log(results);
callback(error, results); connection.end();
}
});
答案 0 :(得分:0)
根据您的评论,您的查询不正确,请检查所需的修改;
// make multiple statements true
var connection = mysql.createConnection({multipleStatements: true});
var query1 = "INSERT INTO abc VALUES("1","2");"; <== VALUES & `;` were missing
var query2 = "INSERT INTO xyz VALUES("3","4");";
var query3 = "INSERT INTO aaa VALUES("5","6");";
var sql = query1+query2+query3;
connection.query(sql, function (error, results, fields) {
if (error) {
console.log("Error occured");
connection.destroy();
throw error;
} else {
// connected!
console.log(results);
callback(error, results); connection.end();
}
});
修改1:如果INSERT OR UPDATE
DUPLICATE KEY
使用以下查询
var query1 = "INSERT INTO abc VALUES("1","2") ON DUPLICATE KEY UPDATE val1="1", val2=2;";