• Qt数据库查询之事务操作


      在做借书系统的时候,用到了事务操作,不会使用qt中事务操作怎么写,查了一些博客帖子,并不起作用,后来发现,在进行事务成功判断时,出现问题,正确代码如下

     if(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();
                            }
                        }
    
                    }
  • 相关阅读:
    HDU4366 Successor 线段树+预处理
    POJ2823 Sliding Window 单调队列
    HDU寻找最大值 递推求连续区间
    UVA846 Steps 二分查找
    HDU3415 Max Sum of MaxKsubsequence 单调队列
    HDU时间挑战 树状数组
    UVA10168 Summation of Four Primes 哥德巴赫猜想
    UESTC我要长高 DP优化
    HDUChess 递推
    HDU4362 Dragon Ball DP+优化
  • 原文地址:https://www.cnblogs.com/tla001/p/6323656.html
Copyright © 2020-2023  润新知