当前位置:Gxlcms > 数据库问题 > Qt数据库查询之事务操作

Qt数据库查询之事务操作

时间:2021-07-01 10:21:17 帮助过:113人阅读

(QSqlDatabase::database().transaction()){ bool res1,res2; query.prepare("update booklist set borrowNum=?,restNum=? where bookId=?"); query.bindValue(0,++borrownum); query.bindValue(1,--restnum); query.bindValue(2,bookId); res1=query.exec(); //qDebug()<<"insert "<<res1; query.prepare("insert into recordlist(bookId,bookName,borrowPeople,borrowTime,backTime,\ backState) values(?,?,?,?,?,?)"); query.bindValue(0,bookId); query.bindValue(1,bookName); query.bindValue(2,userName); query.bindValue(3,borrowTime); query.bindValue(4,""); query.bindValue(5,0); res2=query.exec(); //qDebug()<<query.lastError(); //qDebug()<<"insert "<<res2; if(res1&&res2){ if(!QSqlDatabase::database().commit()){ QMessageBox::critical(this,"Error","操作失败,将要回滚"); if(!QSqlDatabase::database().rollback()){ QMessageBox::critical(this,"Error","回滚失败"); } }else{ doShowBackResult(); //qDebug()<<"show "; } }else{ qDebug()<<query.lastError(); QMessageBox::critical(this,"Error","操作失败,将要回滚"); if(!QSqlDatabase::database().rollback()){ QMessageBox::critical(this,"Error","回滚失败"); qDebug()<<QSqlDatabase::database().lastError(); } } }

 

Qt数据库查询之事务操作

标签:debug   bin   代码   rest   val   show   数据库   .com   use   

人气教程排行