QSqlQuery: remove a record

  • I tried removing some records from QSqlQuery using query.record().remove(pos) but it doesn't work at all. Is there any way to remove the record from QSqlQuery? I know about QSqlQueryModel, but I would like to use QSqlQuery, because it works fine for my application.
    #include "QtSql/QtSql"
    #include "QtSql/QSqlRecord"

    QString sql_str;
    sql_str = "select * from param";
    QSqlQuery query;

    int index=0;


        ui->tableWidget_2->setItem(index,0,new QTableWidgetItem(query.record().value(0).toString()));
        ui->tableWidget_2->setItem(index,1,new QTableWidgetItem(query.value(1).toString()));

  • Hi, QSqlRecord::remove() doesn't remove any records from the database or from your query (it just removes a field from the record).

    If you want to delete stuff from your database you can use QSqlQuery::exec().

  • Here is an example on how to use the exec.

    @bool TickedgeDatabase::deleteModel(int modelID)
    bool bstatus = false;
    QString filter;
    QString str;
    if (!handle) {
    errorMessage = tr("delete Mode - handle is not initialized");
    qWarning() << errorMessage;
    return false;

    QSqlQuery query(*handle);
    filter = "modelID='" + QString::number(modelID) + "'";
    str = "delete from models where " + filter;
    bstatus = query.prepare(str);
    if (bstatus == false) {
    qWarning() << "Delete from model table failed in prepare " << FILE << LINE;
    sqlError = query.lastError();
    goto done;
    bstatus = query.exec();
    if (bstatus == false) {
    sqlError = query.lastError();
    errorMessage = sqlError.databaseText();
    qWarning() << errorMessage;
    return bstatus;

  • Thanks for replies, but I don't want to delete stuff from my database.
    Actually, I need to remove records from my query and don't touch database. I'd like to organize somewhat like filter on the program level not database level.
    I've already got all data in my query and I think it's irrational to execute the query to the database each time.

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.