How can I set the id field of an inserted QSqlRecord?
-
I have a QSqlRecord with these initializations:
class PasswordFields { QSqlField f1; QSqlField f2; QSqlField f3; QSqlField f4; QSqlRecord rec; public: PasswordFields(int id, const QString& password, const QDateTime& beginDate,int fkLocation ) : f1{"id", QMetaType(QMetaType::Int)}, f2{"password", QMetaType(QMetaType::QString)}, f3{"beginDate", QMetaType(QMetaType::QDateTime)}, f4("fkLocation", QMetaType{ QMetaType::Int }) { if (id > -1) { f1.setValue(id); rec.append(f1); } f2.setValue(password); f3.setValue(beginDate); f4.setValue(fkLocation); rec.append(f2); rec.append(f3); rec.append(f4); } void setId(int pk) { f1.setValue(pk); rec.append(f1); auto id = rec.value(0).toInt(); auto pass = rec.value(1).toString(); }
No matter what I do, after I insert the record in the model, there is no way to set the primary key even though I obtain it via
auto id = model->query().lastInsertId().toInt();
the pk remains 0 even after calling setId(int pk)!!
What am I doing wrong?
-
@jdent It now works:
class PasswordFields { QSqlField f1; QSqlField f2; QSqlField f3; QSqlField f4; QSqlRecord rec; public: PasswordFields(int id, const QString& password, const QDateTime& beginDate,int fkLocation ) : f1{"id", QMetaType(QMetaType::Int)}, f2{"password", QMetaType(QMetaType::QString)}, f3{"beginDate", QMetaType(QMetaType::QDateTime)}, f4("fkLocation", QMetaType{ QMetaType::Int }) { if (id > -1) { f1.setValue(id); rec.append(f1); } f2.setValue(password); f3.setValue(beginDate); f4.setValue(fkLocation); rec.append(f2); rec.append(f3); rec.append(f4); } void setId(int pk) { f1.setValue(pk); rec.insert(0, f1); }
The change was in rec.insert(0,f1) instead of rec.append(f1)... why???
-
-
@jdent said in How can I set the id field of an inserted QSqlRecord?:
The change was in rec.insert(0,f1) instead of rec.append(f1)... why???
Because insert does insert it at the specified position and append adds it to the end. Don't know how more obvious to make the function names...
-
@Christian-Ehrlicher you misunderstand... of course I understand the difference between insert and append! That's not the point.
What I don't understand is why programming this method works differently:
void setId(int pk)
{
f1.setValue(pk);
rec.insert(0, f1); // THIS updates the primary key of the object in memory
}void setId(int pk)
{
f1.setValue(pk);
rec.append(f1); // THIS DOES NOT update the primary key of the object in memory
}Why??
-
@jdent said in How can I set the id field of an inserted QSqlRecord?:
Why??
Because, as the function names tell you, f1 is appended to your QSqlRecord (which only contains values).
-
@Christian-Ehrlicher Again my point is not being understood. See this code and maybe it can become clear to all finally:
class PasswordFields { QSqlField f1; QSqlField f2; QSqlField f3; QSqlField f4; QSqlRecord rec; public: PasswordFields(int id, const QString& password, const QDateTime& beginDate,int fkLocation ) : f1{"id", QMetaType(QMetaType::Int)}, f2{"password", QMetaType(QMetaType::QString)}, f3{"beginDate", QMetaType(QMetaType::QDateTime)}, f4("fkLocation", QMetaType{ QMetaType::Int }) { if (id > -1) { f1.setValue(id); rec.append(f1); } f2.setValue(password); f3.setValue(beginDate); f4.setValue(fkLocation); rec.append(f2); rec.append(f3); rec.append(f4); } void setId(int pk) { f1.setValue(pk); rec.insert(0, f1); }
When I call PasswordFields with id > -1, then f1 is set in QSqlRecord by means of a
f1.setValue(id); rec.append(f1);
otherwise f1 remains unset. When after insertion of the record I finally obtain the QSqlRecord's primary key, I call
pwd.setId(5);
which does nothing to update the f1 field in the record if we use
f1.setValue(id); rec.append(f1);
On the other hand, if setId() is
f1.setValue(id); rec.insert(0,f1);
then the f1 field is successfully updated in the record in memory!
See the difference? Why?
-
Again - your record is ordered. When you access the record with QSqlRecord::value(int) you will get a different value depending on appending or inserting. But you don't show the code you're using the created record. Also I don't understand why you store your data twice - once in separate QSqlFields and once in QSqlRecord.