What I want to do is to query.exec() a DELETE statement, and if delete is successful I want to display the updated table, else display a QMessageBox saying that the name did not match.
I was under the impression that if DELETE statement does not find an entry, query.exec() will be FALSE. But it is always true no matter what I enter for the name. Is there a work around this problem? Thanks all...
So the code I currently have is:
bool ok;
QInputDialog *dialog = new QInputDialog;
QString name = dialog->getText((QWidget*)this->parent(), tr("Enter Buyer to Delete:"),
tr("Buyer Name:"), QLineEdit::Normal,
"", &ok);
QSqlTableModel *model1;
QSqlQuery query;
query.prepare("DELETE from buyers WHERE name=:name");
query.addBindValue(name);
bool x = query.exec(); //<----PROBLEM: EVEN WITH INCORRECT name,
qDebug() << x; // query.exec() RETURNS true ALTHOUGH delete
if (!x) // IS SUPPOSED TO FAIL
{
QMessageBox box;
box.setInformativeText("No buyer found with name matching " + name);
box.exec();
}
model1 = new QSqlTableModel;
model1->setTable("buyers");
model1->select();
model1->setHeaderData(0, Qt::Horizontal, tr("ID"));
model1->setHeaderData(1, Qt::Horizontal, tr("Name"));
model1->setHeaderData(2, Qt::Horizontal, tr("Location"));
model1->setHeaderData(3, Qt::Horizontal, tr("Phone"));
model1->setHeaderData(4, Qt::Horizontal, tr("Email"));
QTableView *view1 = new QTableView;
view1->setWindowTitle("Buyer List updated");
view1->setModel(model1);
view1->show();
Aucun commentaire:
Enregistrer un commentaire