apicloud 执行多条sql语句 db.transaction事务回滚 怎么写?
function test ()
{
var db = api.require('db');
db.transaction({
name: 'test',
operation: 'begin'
}, function(ret, err){
if( ret.status ){
alert( JSON.stringify( ret )+" begin1" );
try
{
//测试回滚 ,这里可以插入成功
var sql = 'insert into Persons(Id_P, LastName, FirstName, Address, City,createTime)values(1,"lastname1","firstname1","address1","city1","'+new Date()+'")';
db.executeSql({
name: 'test',
sql: sql
}, function(ret, err){
if(ret.status){
api.alert({msg:'operate data success 1.'});
console.log(2);
}else{
api.alert({msg:err.msg});
db.transaction({
name: 'test',
operation: 'rollback'
}, function(ret, err){
if( ret.status ){
alert( JSON.stringify( ret ) +" rollback1" );
}else{
alert( JSON.stringify( err ) +" rollback2" );
}
});
}
});
//测试回滚 ,这里可以插入失败【5个字段,6个值】
sql = 'insert into Persons(LastName, FirstName, Address, City,createTime)values(1,"lastname2","firstname2","address2","city2","'+ new Date()+'")';
db.executeSql({
name: 'test',
sql: sql
}, function(ret, err){
if(ret.status){
api.alert({msg:'operate data success 2.'});
console.log(3);
}else{
api.alert({msg:err.msg});
db.transaction({
name: 'test',
operation: 'rollback'
}, function(ret, err){
if( ret.status ){
alert( JSON.stringify( ret ) +" rollback1" );
}else{
alert( JSON.stringify( err ) +" rollback2" );
}
});
}
});
db.transaction({
name: 'test',
operation: 'commit'
}, function(ret, err){
if( ret.status ){
alert( JSON.stringify( ret )+" commit1" );
}else{
alert( JSON.stringify( err )+" commit2" );
}
});
}
catch(err)
{
db.transaction({
name: 'test',
operation: 'rollback'
}, function(ret, err){
if( ret.status ){
alert( JSON.stringify( ret ) +" rollback1" );
}else{
alert( JSON.stringify( err ) +" rollback2" );
}
});
}
}else{
alert( JSON.stringify( err )+" begin2" );
}
});
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论