如何在sqlite数据库中的c ++中一次保存或更新多个表?

问题描述 投票:0回答:1
       QSqlQuery qry4;
       QSqlQuery qry5;
       QSqlQuery qry6;



 qry3.prepare("INSERT INTO apra(Source,Medium,Subject,'Date & Time received document','Date & Time RRO forwarded to HOO','ControlNo.') values('"+src+"','"+med+"','"+key+"','"+one+"','"+one+"','"+res+"')");
       qry4.prepare("INSERT INTO bag(Source,Medium,Subject,'Date & Time received document','Date & Time RRO forwarded to HOO','ControlNo.') values('"+src+"','"+med+"','"+key+"','"+one+"','"+one+"','"+res+"')");
       qry5.prepare("INSERT INTO ben(Source,Medium,Subject,'Date & Time received document','Date & Time RRO forwarded to HOO','ControlNo.') values('"+src+"','"+med+"','"+key+"','"+one+"','"+one+"','"+res+"')");
       qry6.prepare("INSERT INTO cent(Source,Medium,Subject,'Date & Time received document','Date & Time RRO forwarded to HOO','ControlNo.') values('"+src+"','"+med+"','"+key+"','"+one+"','"+one+"','"+res+"')");


 if(qry3.exec()){
           QMessageBox::information(this,"Database","qry3");
       }else{
           QMessageBox::critical(this,tr("error::"),qry3.lastError().text());
       }
       if(qry4.exec()){
           QMessageBox::information(this,"Database","qry4");
       }else{
           QMessageBox::critical(this,tr("error::"),qry4.lastError().text());
       }
       if(qry5.exec()){
           QMessageBox::information(this,"Database","qry5");
       }else{
           QMessageBox::critical(this,tr("error::"),qry5.lastError().text());
       }
       if(qry6.exec()){
           QMessageBox::information(this,"Database","qry6");
       }else{
           QMessageBox::critical(this,tr("error::"),qry6.lastError().text());
       }

我想知道一种更新SQLite中表格的简单方法。因为此方法还会弹出3次信息提示,如果我有10个查询或更多查询该怎么办,这将很耗时且不理想。

c++ sqlite
1个回答
0
投票
qry3.prepare("INSERT INTO apra(Source,Medium,Subject,'Date & Time received document','Date & Time RRO forwarded to HOO','ControlNo.') values(?,?,?,?,?,?)"); qry4.prepare("INSERT INTO bag(Source,Medium,Subject,'Date & Time received document','Date & Time RRO forwarded to HOO','ControlNo.') values(?,?,?,?,?,?)"); qry5.prepare("INSERT INTO ben(Source,Medium,Subject,'Date & Time received document','Date & Time RRO forwarded to HOO','ControlNo.') values(?,?,?,?,?,?)"); qry6.prepare("INSERT INTO cent(Source,Medium,Subject,'Date & Time received document','Date & Time RRO forwarded to HOO','ControlNo.') values(?,?,?,?,?,?)");

您可以按如下所示对其进行循环:

bool success = true;
for (auto& q_wrapper : std::initializer_list<std::reference_wrapper<QSqlQuery>>{qry3, qry4, qry5, qry6}) {
  QSqlQuery& q = q_wrapper.get();
  q.addBindValue(src);
  q.addBindValue(med);
  q.addBindValue(key);
  q.addBindValue(one);
  q.addBindValue(one);
  q.addBindValue(res);
  success &&= q.exec();
  if (!success) {
    QMessageBox::critical(this,tr("error::"),q.lastError().text())
    break;
  }
}

if (success) {
  QMessageBox::information(this,"Database","all queries succeeded");
}

可能的改进:

切换到:named placeholders并将所有占位符存储在QMap<QString, QVariant>中。

    包装交易中的所有内容。这使您可以避免部分更新。 (使用QSqlDatabase().transaction()开始交易,然后调用QSqlDatabase().commit()(如果成功)或QSqlDatabase().rollback()(如果成功!)
热门问题
推荐问题
最新问题