gpt4 book ai didi

mysql - Sqljocky,多组参数的事务查询

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

我正在使用 sqljocky 将数据插入 MySQL 数据库。我需要先截断一个表,然后在其中插入多行。我会在单个事务中执行此操作,但 sqljocky 现在似乎根本不支持此操作(或者我可能是 dart 和 sqljocky 的新手)。

我找到的解决方案如下,但我想知道是否有更好的解决方案。

// Start transaction
pool.query('START TRANSACTION').then((r) {

// Truncate table
pool.query('TRUNCATE myTable').then((r) {

// Prepare statement to insert new data
pool.prepare('REPLACE INTO myTable (Id, Name, Description) VALUES (?,?,?)').then((query) {

// Execute query inserting multiple rows
query.executeMulti(myArrayValues).then((results) {

// Other stuff here
pool.query('COMMIT').then((r) {
...

老实说,我仍然想知道这段代码是否真的执行事务查询!

最佳答案

下面是使用事务支持重写的相同代码:

// Start transaction
pool.startTransaction().then((trans) {

// Delete all from table
trans.query('DELETE FROM myTable WHERE 1=1').then((r) {

// Prepare statement
trans.prepare('REPLACE INTO myTable (Id, Name, Description) VALUES (?,?,?)').then((query) {

// Execute query inserting multiple rows
query.executeMulti(myArrayValues).then((results) {
// Stuff here

// Commit
trans.commit().then((r) { ...

关于mysql - Sqljocky,多组参数的事务查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21994393/

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