gpt4 book ai didi

mysql - 如何运行动态查询数组的事务

转载 作者:行者123 更新时间:2023-11-29 18:35:58 26 4
gpt4 key购买 nike

我有一个动态查询数组,示例:-

arrQueries=["update table1 set field1='val1',field2='val2' where 
id=5;","update table1 set field3='val3',field4='val4' where id=10;" ];

如何通过循环数组来运行事务?

最佳答案

我使用了node-mysql-transaction

    var transaction = require('node-mysql-transaction');
var trCon = transaction({connection: [mysql.createConnection, {
// mysql connection config
user: ...,
password: ...,
database:...,
host:..
}], dynamicConnection: 32, timeout: 600});
arrQueries=["update table1 set field1='val1',field2='val2' where
id=5;","update table1 set field3='val3',field4='val4' where id=10;" ];

var chain = trCon.chain();
chain.
on('commit', function () {
console.log('commit');
return res.send("commit");

}).
on('rollback', function (err) {
console.log('rollback');
return res.send("rollback");
});

for (var i = 0; i < arrQueries.length; i += 1) {
chain.query(arrQueries[i]);
}

关于mysql - 如何运行动态查询数组的事务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45286389/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com