如何从sqlite数据库中删除特定行并与qlineedit绑定?

时间:2017-03-02 13:34:56

标签: sql qt sqlite prepared-statement

我可以使用手动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.");
}

查询已执行但未删除。我做错了什么?

1 个答案:

答案 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();