Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QSqlQuery: remove a record

QSqlQuery: remove a record

Scheduled Pinned Locked Moved General and Desktop
4 Posts 3 Posters 2.8k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    Evgenij1
    wrote on last edited by
    #1

    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;
    query.prepare(sql_str);
    query.exec();

    int index=0;

    query.seek(-1);

    while(query.next()){
        query.record().clear();
        query.record().clearValues();
        query.record().remove(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()));
        index++;
    }@
    
    1 Reply Last reply
    0
    • hskoglundH Offline
      hskoglundH Offline
      hskoglund
      wrote on last edited by
      #2

      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().

      1 Reply Last reply
      0
      • D Offline
        D Offline
        DBoosalis
        wrote on last edited by
        #3

        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;
        }
        done:
        return bstatus;
        @

        1 Reply Last reply
        0
        • E Offline
          E Offline
          Evgenij1
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved