Multiple insertion using QSqlTableModel
Hi,
I am trying toinsert multiple rows (you can actually call it records) in a database using QSqlTable model but cannot get it working. Can someone please help ?
Following is the code which works.
Code:
for(int i = 0 ; i < n; ++i){ // I want to insert some 10 rows
tableModel.select();
int rc = tableModel.rowCount();
tableModel.insertRow(rc);
rec = tableModel.record(rc);
tableModel.setRecord(rc,rec);
tableModel.submitAll();
}
I have tired various other methods but all of them are failing. Can someone please suggest the best way to do this?
Thanks a lot.
Re: Multiple insertion using QSqlTableModel
How about this?
Code:
for( int i = 0 ; i < n; ++i ) {
rec.setValue( "nameid", nameIds.at( i ) );
rec.setValue( "groupid", currentGroupId );
tableModel.insertRecord( -1, rec );
}
tableModel.submitAll();
Re: Multiple insertion using QSqlTableModel
No. Application crashes with the above code.
But this works.
Code:
for( int i = 0 ; i < n; ++i ) {
tableModel.select();//This needs to be added
rec.setValue( "nameid", nameIds.at( i ) );
rec.setValue( "groupid", currentGroupId );
tableModel.insertRecord( -1, rec );
}
tableModel.submitAll();
Thanks a lot.
Re: Multiple insertion using QSqlTableModel
Does this work?
Code:
tableModel.select();
for( int i = 0 ; i < n; ++i ) {
...
}
tableModel.submitAll();
or
Code:
tableModel.select();
for( int i = 0 ; i < n; ++i ) {
...
tableModel.submitAll();
}