I can create a database row, but cannot update it.
-
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 -
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 issueSqlite 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!
-
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); -
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 issueSqlite 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!