How to use setData on inserted row in QSqlTableModel
I want to insert a new record into a QSqlTableModel with insertRow. How can I use QSqlTableModel::setData() when I use QSqlTableModel::insertRow()?
How can I set the data for the new inserted row. QSqlTableModel::setData needs an index to set the data, but how can I get the index?
Or is there another way to do this?
JonB last edited by JonB
m_accountModel->insertRow(0); QModelIndex *index = m_accountModel->index(0, 0); m_accountModel->setData(index, "Here");
How can I achieve that the new inserted data re written to the database?
m_accountModel->setEditStrategy(QSqlTableModel::OnManualSubmit); m_accountModel->insertRow(0); QModelIndex *index = m_accountModel->index(0, 0); m_accountModel->setData(index, "Here"); m_accountModel->submitAll();
This doesn't write the data to the database. What am I doing wrong here?
In principle outline looks fine. My guess: check the return from the
setData(), I only gave example, maybe yours does not accept anything/a string in that column, and (most likely) check
submitAll(), if the newly inserted row (which here has only one column initialized if at all) is not acceptable for a row in the database it will get rejected.
@JonB I found the reason why submitAll() did not write the data to the database table. It was because I didn't set a value for the primary key in the record. Now it works fine.
Thank you very much for your help.
JonB last edited by JonB
QSqlTableModelwill let you insert into it an incomplete row with columns unset or set to bad value. But then when you
submit()the database will barf on the
INSERTstatement, which is what you are seeing. It is a good idea to check/listen for all warning output from the db so that you know what is going on, I subclass/set handlers on all
QSql...classes I use for that.
To add to the previous, I practically dropped use of setData() in favour of insertRecord().
So what I usually do (but my use is never performance oriented):
QSqlRecord rec=record(); //returns me empty, well formatted record //here is filling the record with data insertRecord(row,rec); // row is position at which I insert
There is also setRecord() method available. Both took lots of headache from me.
+1 for using the
QSqlRecord-level stuff. Nicer than
But the "empty record" is not necessarily acceptable for database
INSERT, you still have to fill in any compulsory columns don't you?
@JonB Depends. In my use cases (again, what works for me doesn't have to work for the others) all I have to take care of are the fields that explicitly can't be null. Indexes are autofilled. As I said, my use cases are quite simple - but then again, QSqlRecord-related stuff has a lot of capabilities I simply don't use.
all I have to take care of are the fields that explicitly can't be null.
Yes. But I think (correct me if wrong) that the
QSqlRecord-level does not check this for you, it lets you leave and then the
UPDATEdb statement errors. That is what I was referring to.
@JonB No, you are correct. As far as I can tell (I probably did a mistake like this in the past just can't recall) attempt to insert a malformed record, be it without necessary fields filled or with a wrong structure will fail. (it is probably also in the docs but can't dive there at this moment to check)
Yes, IIRC in (my beloved) C# data/database handling classes, it stopped you putting an invalid-type-value into its
QRecordinternally before db involved, so this sort of issue was picked up before sending to db, which is nice. Obviously one could write one's own subclass of
QRecordto achieve this, I think.
I'm working now with setData instead of QSqlRecord. I found it to be cleaner especially when updating values.