我可以使用手动SQL查询命令删除特定行。但无法从QLineEdit
删除。如何与QLineEdit
绑定?
这是我的代码:
person_name = ui->txt_UserName->text ();
mobile_number = ui->txt_Pass->text ();
//delete values
QString deleteStatement = "DELETE FROM phonebook_info WHERE user_name = ':person_name'";
query->bindValue (":person_name", person_name);
query->exec (deleteStatement);
if(query->exec ()){
QMessageBox::information (this, "Information!", "Row Deleted.", QMessageBox::Ok);
ui->statusBar->showMessage ("Row Deleted.");
} else {
QMessageBox::critical (this, "Information!", "Row not Deleted.", QMessageBox::Ok);
ui->statusBar->showMessage ("Row not Deleted.");
}
查询已执行但未删除。我做错了什么?
答案 0 :(得分:1)
语法看起来不对,您拨打query->exec()
两次。这是您应该如何运行预准备语句:
QSqlQuery query = new QSqlQuery(databaseInstance);
bool ok = query->prepare(deleteStatement);
if (!ok) {
qWarning() << "SQL error:" << deleteStatement;
}
query->bindValue(":person_name", person_name);
query->exec();