Qt中sqlite事务实例

sqlite事务实例:

第一种方法:

 QString tempStr1="";
    QDateTime startT=QDateTime::currentDateTime();
    query.exec("BEGIN TRANSACTION;");//开始事务。使用BEGIN也可以
    bool ok1=false;
    //第一种方法:使用exec,虽然使用了事务,但是还是单条提交
    while((!subEndDateTime.isNull())&&subEndDateTime.toTime_t()<=endDateTime.toTime_t())
    {
        QString s_str = startDateTime.toString("yyyy-MM-dd HH:mm:ss");
        QString e_str = subEndDateTime.toString("yyyy-MM-dd HH:mm:ss");
        tempStr1="update cardetectinfo set groupTag='"+QString::number(groupTag)+"' where cameraName='"+cameraName+"' and checkPointName='"+checkPointName+"' and countTime>='" +s_str+"' and countTime<='"+e_str+"';";
        ok1= query.exec(tempStr1);
        startDateTime=subEndDateTime.addSecs(1);
        subEndDateTime=startDateTime.addSecs(sec_sum);//这个标记的结束时间
        groupTag++;
    }
     if(ok1 )
    {
        query.exec("COMMIT");//提交
    }else
    {
        query.exec("ROLLBACK");//回滚
    }

第二种方法:

QString tempStr1="";
    QDateTime startT=QDateTime::currentDateTime();
    query.exec("BEGIN TRANSACTION;");//开始事务。使用BEGIN也可以
    bool ok1=false;
  while((!subEndDateTime.isNull())&&subEndDateTime.toTime_t(<=endDateTime.toTime_t())
                {
                  QString s_str = startDateTime.toString("yyyy-MM-dd HH:mm:ss");
                  QString e_str = subEndDateTime.toString("yyyy-MM-dd HH:mm:ss");
                   query.prepare("update cardetectinfo set groupTag=? where cameraName=? and checkPointName=? and countTime>=? and countTime<=?");
                    query.bindValue(0,groupTag);
                        query.bindValue(1,cameraName);
                        query.bindValue(2,checkPointName);
                        query.bindValue(3,s_str);
                       query.bindValue(4,e_str);
                  ok1= query.exec();
                   startDateTime=subEndDateTime.addSecs(1);
                  subEndDateTime=startDateTime.addSecs(sec_sum);//这个标记的结束时间
                   groupTag++;
              }
                if(ok1 )
                {
                    query.exec("COMMIT");//提交
                }else
                {
                    query.exec("ROLLBACK");//回滚
                }

但是多语句提交一直未搞定,期待解决!

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值