PDA

View Full Version : Multiple insertion using QSqlTableModel



munna
9th May 2006, 20:00
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.



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);
rec.setValue(QString("nameid"),QVariant(nameIds.at(i)));
rec.setValue(QString("groupid"),QVariant(currentGroupId));
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.

jacek
9th May 2006, 20:11
How about this?
for( int i = 0 ; i < n; ++i ) {
QSqlRecord rec( tableMode.record() );
rec.setValue( "nameid", nameIds.at( i ) );
rec.setValue( "groupid", currentGroupId );
tableModel.insertRecord( -1, rec );
}
tableModel.submitAll();

munna
9th May 2006, 20:29
No. Application crashes with the above code.

But this works.



for( int i = 0 ; i < n; ++i ) {
tableModel.select();//This needs to be added
QSqlRecord rec( tableModel.record() );
rec.setValue( "nameid", nameIds.at( i ) );
rec.setValue( "groupid", currentGroupId );
tableModel.insertRecord( -1, rec );
}
tableModel.submitAll();


Thanks a lot.

jacek
9th May 2006, 21:20
Does this work?

tableModel.select();
for( int i = 0 ; i < n; ++i ) {
...
}
tableModel.submitAll();
or

tableModel.select();
for( int i = 0 ; i < n; ++i ) {
...
tableModel.submitAll();
}