如何使用node js在error部分编写查询

nbnkbykc  于 2021-06-23  发布在  Mysql
关注(0)|答案(1)|浏览(322)

我正在使用node js批量上传到mysql db。我正在插入多个行,如果在执行过程中发生任何错误,我必须更新表中的一列。我该怎么做呢。

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(); 
    } 
});
xfyts7mz

xfyts7mz1#

根据您的意见,您的查询不正确,请检查所需的修改;

// 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;";

相关问题