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. No fields to update when using insertRecord
Forum Updated to NodeBB v4.3 + New Features

No fields to update when using insertRecord

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 2 Posters 1.1k Views 2 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.
  • M Offline
    M Offline
    Mark81
    wrote on 11 Apr 2020, 12:44 last edited by Mark81 4 Nov 2020, 12:56
    #1

    I'm not sure if I'm using QSqlTableModel correctly.
    I'm able to fetch data from the db but I cannot write back.

       _model = new QSqlTableModel(this, QSqlDatabase::database("myDb"));
       _model->setTable("myTable");
       _model->select();
       
       ...
    
       QSqlRecord record;
       record.setValue("sn", ui->lineSN->text()); // this is the primary key
       record.setValue("year", ui->dateProd->date().year());
       record.setValue("month", ui->dateProd->date().month());
       record.setValue("day", ui->dateProd->date().day());
       // ...set all the other fields
    
       for (int i = 0; i < record.count(); i++) record.setGenerated(i, true);
    
       int row = findRow(ui->lineSN->text()); // if the key exists -> update row
       _model->insertRecord(row, record);
       //_model->submitAll(); // not sure if I need this
       qDebug() << _model->lastError();
    

    insertRecord fails (i.e. return false) and I receive this error:

    "No Fields to update"

    This is the same even if I uncomment the commitAll line.
    What am I missing?

    1 Reply Last reply
    0
    • M Offline
      M Offline
      Mark81
      wrote on 11 Apr 2020, 12:56 last edited by
      #2

      I changed my code and now it works. It's counter-intuitive and I'm not sure if it's correct - but as said it works:

      ```
      int row = findRow(ui->lineSN->text());
      
      if (row < 0)
      {
          row = _model->rowCount();
          _model->insertRow(row);
      }
      QSqlRecord record = _model->record(row);
      record.setValue("sn", ui->lineSN->text());
      record.setValue("year", ui->dateProd->date().year());
      record.setValue("month", ui->dateProd->date().month());
      record.setValue("day", ui->dateProd->date().day());
      for (int i = 0; i < record.count(); i++) record.setGenerated(i, true);
      
      _modelManufacturing->setRecord(row, record);
      _modelManufacturing->submitAll();
      
      1 Reply Last reply
      0
      • K Offline
        K Offline
        Kent-Dorfman
        wrote on 11 Apr 2020, 20:19 last edited by
        #3

        what are you attempting, an insert, or an update? They are DIFFERENT operations as far as the database in concerned. SQL UPDATE fails by design if it doesn't refer to an existing row. INSERT indiscriminately adds a row as long as it generates no duplicate key or referential integrity errors.

        1 Reply Last reply
        2

        1/3

        11 Apr 2020, 12:44

        • Login

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