2015-12-08 22 views
0

我想更新我的小說表。我必須上課。在QT中更新數據庫中的數據C++

fictioninner.cpp

bool FictionInner :: updateMaterials(int fic_id,int copies, int shelf,int edition) 
{ 
    QSqlQuery query ; 

    query.prepare("update fiction set no_of_cpy =:copy,shelf_no=:shelf,edition=:edition" 
        " where fic_bk_id=:id"); 

    query.bindValue(":copy",copies); 
    query.bindValue(":shelf",shelf); 
    query.bindValue(":edition",edition); 
    query.bindValue(":id",fic_id); 
    return flag = query.exec(); 
} 

fictionsection.cpp

void FictionSection::on_btnEdit_clicked() 
{ 

    QString noOfCpy = ui->lneditMngCpy->text(); 
    int copy; 
    if(!noOfCpy.isNull()) 
    { 
      copy = noOfCpy.toInt(); 
    } 
     int shelf = ui->spinbxMngShlf->value(); 

     int edition= ui->spinbxMngEdtn->value(); 

     QString ficId = ui->lneditMngFicId->text(); 
      int fictionId; 
      if(!ficId.isNull()) 
      { 
       fictionId = ficId.toInt(); 
      } 

     FictionInner fiction; 

    // assigning bool value to flag variable after running the query 
     flag = fiction.updateMaterials(fictionId,copy,shelf,edition); 
     if(flag == true) 
    { 
    QMessageBox :: information(this,"Successfull","Values updated successfully",QMessageBox::Ok); 
    } 
else 
{ 
    QMessageBox :: critical(this,"Error","Couldn't update the values"); 
} 


} 

數據未在表中編輯。標誌布爾值返回false。所有數據都以INT格式存儲在數據庫中。我無法弄清楚我的問題。預先感謝您

+0

http://doc.qt.io/qt-4.8/qsqlquery.html#lastError <=使用它獲取信息。 – Mat

+0

我使用qDebug()<<檢查了每個變量的值。所有的打印int值。所以沒有空。 – user3279893

+0

你得到的錯誤是什麼?在不知道調用exec的確切錯誤觸發的情況下,您無法調試您的問題。 – Mat

回答

0

在更新您的記錄之前,您應該打開您的數據庫,然後使用開放數據庫對象構造QSqlquery對象。

+0

我在項目開始時已連接到數據庫。我檢查了是否使用if(dbconopen()){}連接到數據庫。它表明Im連接。 – user3279893

+0

做一些類似於:QSqlQuery query(database); – Ankur