如果 select 返回 0 行则插入数据
我当前有一个事务尝试将数据插入表中。如果数据已在表中,则会引发约束失败错误,并运行选择来获取 ID。
t2.executeSql('INSERT INTO books (book) VALUES (?);',
[record],
function (t2, r) { // SQL_successfulCallback
record = r.insertId;
},
function (t2, err) { // SQL_errorCallback
if (err.message !== 'constraint failed') { // insert failed because of other
// reason - fail transaction
console.log('Insert SQL error ' + err.code + ' - ' + err.message + '.');
return true;
} else { // insert failed because data was already in the table
t2.executeSql('SELECT bookID FROM books WHERE book=?',
[record],
function (t, r) { // SQL_successfulCallback
record = r.rows.item(0).classificationID;
},
function (t, err) { // SQL_errorCallback
console.log('Lookup SQL error ' + err.code + ' - ' + err.message + '.');
return true;
}
);
return false;
}
}
);
我想加快交易速度,所以我想先看看数据是否在表中。如果不是,则插入它...
t2.executeSql('SELECT bookID FROM books WHERE book=?',
[record],
function (t2, r) { // SQL_successfulCallback
if (r.rows.length !== 0) {
record = r.rows.item(0).bookID;
} else {
t2.executeSql('INSERT INTO books (book) VALUES (?);',
[record],
function(t2, r){ // SQL_successfulCallbac
record = r.insertId;
},
function (t2, err) { // SQL_errorCallback
if (err.message !== 'constraint failed') { // insert failed because of other
// reason - fail transaction
console.log('Insert SQL error ' + err.code + ' - ' + err.message + '.');
return true;
} else { // insert failed because data was already in the table
return false;
}
}
);
}
},
function (t, err) { // SQL_errorCallback
console.log('Lookup SQL error ' + err.code + ' - ' + err.message + '.');
return true;
}
);
...但它不起作用。该事务运行所有选择,然后执行插入。我怎样才能使第二种方法起作用?
I currently have a transaction that attempts to insert data into a table. If the data is already in the table a constraint failed error is raised and a select is run to get the ID.
t2.executeSql('INSERT INTO books (book) VALUES (?);',
[record],
function (t2, r) { // SQL_successfulCallback
record = r.insertId;
},
function (t2, err) { // SQL_errorCallback
if (err.message !== 'constraint failed') { // insert failed because of other
// reason - fail transaction
console.log('Insert SQL error ' + err.code + ' - ' + err.message + '.');
return true;
} else { // insert failed because data was already in the table
t2.executeSql('SELECT bookID FROM books WHERE book=?',
[record],
function (t, r) { // SQL_successfulCallback
record = r.rows.item(0).classificationID;
},
function (t, err) { // SQL_errorCallback
console.log('Lookup SQL error ' + err.code + ' - ' + err.message + '.');
return true;
}
);
return false;
}
}
);
I want to speed the transaction up so I thought I would see if the data was in the table first. If it isn't then insert it...
t2.executeSql('SELECT bookID FROM books WHERE book=?',
[record],
function (t2, r) { // SQL_successfulCallback
if (r.rows.length !== 0) {
record = r.rows.item(0).bookID;
} else {
t2.executeSql('INSERT INTO books (book) VALUES (?);',
[record],
function(t2, r){ // SQL_successfulCallbac
record = r.insertId;
},
function (t2, err) { // SQL_errorCallback
if (err.message !== 'constraint failed') { // insert failed because of other
// reason - fail transaction
console.log('Insert SQL error ' + err.code + ' - ' + err.message + '.');
return true;
} else { // insert failed because data was already in the table
return false;
}
}
);
}
},
function (t, err) { // SQL_errorCallback
console.log('Lookup SQL error ' + err.code + ' - ' + err.message + '.');
return true;
}
);
...but it doesn't work. This transaction runs all the selects then does the inserts. How can I make the second method work?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我假设事务正在对请求进行排队。因此,您的队列将如下所示:
Select 1
Select 2
Select 3
然后,当您提交事务时,在第一次调用后将如下所示。
Select 2
Select 3
Insert 1
Insert 2
Insert 3
发生这种情况是因为调用插入的函数在选择运行后执行,并且直到事务提交但选择已经注册后才会发生这种情况。
为了使其成为
select1
insert1
select2
insert2
我将为每个 select 语句创建一个单独的事务。
I'm assuming the transaction is queuing the request. So your queue will look like this
Select 1
Select 2
Select 3
Then when you commit your transaction looks like this after the first call.
Select 2
Select 3
Insert 1
Insert 2
Insert 3
This is happening because the the functions to call the inserts are exected after the select is ran and this doesn't happen until the transaction commits but the selects have already be registered.
In order to get it to be
select1
insert1
select2
insert2
I would create a separate transaction for each select statement.