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. I can create a database row, but cannot update it.
Forum Updated to NodeBB v4.3 + New Features

I can create a database row, but cannot update it.

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 1 Posters 252 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.
  • D Offline
    D Offline
    Demolishun
    wrote on last edited by Demolishun
    #1

    Qt 5.15.2
    SQlite database.

    I am using a QSqlLTableModel (m_datapointModel):

    void TankDatabase::insert_thicknessData(int inindex, double xpos, double ypos, double zpos, double thickness1, double thickness2, double thickness3)
    {
        auto record = m_datapointModel->record();
    
        record.setValue("rxpos", xpos);
        record.setValue("rypos", ypos);
        record.setValue("rzpos", zpos);
        record.setValue("rreading1", thickness1);
        record.setValue("rreading2", thickness2);
        record.setValue("rreading3", thickness3);
        record.setValue("rdatetime", QDateTime::currentDateTime());
    
        bool success = false;
    
        qDebug() << inindex << record;
    
        if(inindex >= 0){
            /*
            for(int ind=0; ind < record.count(); ++ind){
                record.setGenerated(ind, false);
            }
            */
    
            success = m_datapointModel->setRecord(inindex, record);
            if(success){
                qDebug() << "m_datapointModel updated existing record" << inindex;
            }
        }
    
        if(!success){
            success = m_datapointModel->insertRecord(-1, record);
            if(success){
                qDebug() << "m_datapointModel inserted new record" << inindex;
            }
        }
    
        if(success){
            //qDebug() << "updating database";
            success = m_datapointModel->submitAll();
            if(!success){
                qDebug() << m_datapointModel->lastError();
            }
    
            //auto db = QSqlDatabase::database(m_fileName);
            //db.commit();
    
        }else{
            qDebug() << "m_datapointModel could not insert/update record:" << inindex << record;
        }
    }
    

    This code inserts fine when the index is -1. But when I try to update a row it throws this error (lastError()):

    QSqlError("20", "Unable to fetch row", "datatype mismatch")
    

    The data types look identical:

    -1 QSqlRecord(8)
     0: QSqlField("pindex", int, tableName: "DataPoint", generated: yes, typeID: 1, autoValue: false, readOnly: false) "0" 
     1: QSqlField("rxpos", double, tableName: "DataPoint", generated: yes, typeID: 2, autoValue: false, readOnly: false) "0" 
     2: QSqlField("rypos", double, tableName: "DataPoint", generated: yes, typeID: 2, autoValue: false, readOnly: false) "0" 
     3: QSqlField("rzpos", double, tableName: "DataPoint", generated: yes, typeID: 2, autoValue: false, readOnly: false) "0" 
     4: QSqlField("rreading1", double, tableName: "DataPoint", generated: yes, typeID: 2, autoValue: false, readOnly: false) "0" 
     5: QSqlField("rreading2", double, tableName: "DataPoint", generated: yes, typeID: 2, autoValue: false, readOnly: false) "0" 
     6: QSqlField("rreading3", double, tableName: "DataPoint", generated: yes, typeID: 2, autoValue: false, readOnly: false) "0" 
     7: QSqlField("rdatetime", int, tableName: "DataPoint", generated: yes, typeID: 3, autoValue: false, readOnly: false) "2022-11-27T17:33:55.596"
    qml: 10
    m_datapointModel inserted new record -1
    10 QSqlRecord(8)
     0: QSqlField("pindex", int, tableName: "DataPoint", generated: yes, typeID: 1, autoValue: false, readOnly: false) "0" 
     1: QSqlField("rxpos", double, tableName: "DataPoint", generated: yes, typeID: 2, autoValue: false, readOnly: false) "0" 
     2: QSqlField("rypos", double, tableName: "DataPoint", generated: yes, typeID: 2, autoValue: false, readOnly: false) "0" 
     3: QSqlField("rzpos", double, tableName: "DataPoint", generated: yes, typeID: 2, autoValue: false, readOnly: false) "0" 
     4: QSqlField("rreading1", double, tableName: "DataPoint", generated: yes, typeID: 2, autoValue: false, readOnly: false) "0" 
     5: QSqlField("rreading2", double, tableName: "DataPoint", generated: yes, typeID: 2, autoValue: false, readOnly: false) "0" 
     6: QSqlField("rreading3", double, tableName: "DataPoint", generated: yes, typeID: 2, autoValue: false, readOnly: false) "0" 
     7: QSqlField("rdatetime", int, tableName: "DataPoint", generated: yes, typeID: 3, autoValue: false, readOnly: false) "2022-11-27T17:33:56.277"
    m_datapointModel updated existing record 10
    
    1 Reply Last reply
    0
    • D Offline
      D Offline
      Demolishun
      wrote on last edited by
      #3

      I was not maintaining the pindex which is primary key. So instead of creating a blank record I needed to pull the record data for the existing before I updated the data:

      auto record = m_datapointModel->record(inindex);  // changing this line fixed the issue
      

      Sqlite was throwing a SQLITE_MISMATCH error due to the pindex being wrong for the row I was updating. I had not set it to anything. Since I am updating I needed the previous rows data.

      So basically, my fault, DOH!

      1 Reply Last reply
      0
      • D Offline
        D Offline
        Demolishun
        wrote on last edited by
        #2

        Thought maybe there was an issue with the date being a string. So I tried this, but it didn't make the error go away:

            auto secdate = QDateTime::currentDateTime().toSecsSinceEpoch();
            record.setValue("rdatetime", secdate);
        
        1 Reply Last reply
        0
        • D Offline
          D Offline
          Demolishun
          wrote on last edited by
          #3

          I was not maintaining the pindex which is primary key. So instead of creating a blank record I needed to pull the record data for the existing before I updated the data:

          auto record = m_datapointModel->record(inindex);  // changing this line fixed the issue
          

          Sqlite was throwing a SQLITE_MISMATCH error due to the pindex being wrong for the row I was updating. I had not set it to anything. Since I am updating I needed the previous rows data.

          So basically, my fault, DOH!

          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