Results 1 to 4 of 4

Thread: Multiple insertion using QSqlTableModel

  1. #1
    Join Date
    Jan 2006
    Posts
    667
    Thanks
    10
    Thanked 80 Times in 74 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default 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.

    Qt Code:
    1. for(int i = 0 ; i < n; ++i){ // I want to insert some 10 rows
    2. tableModel.select();
    3. int rc = tableModel.rowCount();
    4. tableModel.insertRow(rc);
    5. rec = tableModel.record(rc);
    6. rec.setValue(QString("nameid"),QVariant(nameIds.at(i)));
    7. rec.setValue(QString("groupid"),QVariant(currentGroupId));
    8. tableModel.setRecord(rc,rec);
    9. tableModel.submitAll();
    10. }
    To copy to clipboard, switch view to plain text mode 

    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.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Multiple insertion using QSqlTableModel

    How about this?
    Qt Code:
    1. for( int i = 0 ; i < n; ++i ) {
    2. QSqlRecord rec( tableMode.record() );
    3. rec.setValue( "nameid", nameIds.at( i ) );
    4. rec.setValue( "groupid", currentGroupId );
    5. tableModel.insertRecord( -1, rec );
    6. }
    7. tableModel.submitAll();
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Jan 2006
    Posts
    667
    Thanks
    10
    Thanked 80 Times in 74 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Multiple insertion using QSqlTableModel

    No. Application crashes with the above code.

    But this works.

    Qt Code:
    1. for( int i = 0 ; i < n; ++i ) {
    2. tableModel.select();//This needs to be added
    3. QSqlRecord rec( tableModel.record() );
    4. rec.setValue( "nameid", nameIds.at( i ) );
    5. rec.setValue( "groupid", currentGroupId );
    6. tableModel.insertRecord( -1, rec );
    7. }
    8. tableModel.submitAll();
    To copy to clipboard, switch view to plain text mode 

    Thanks a lot.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Multiple insertion using QSqlTableModel

    Does this work?
    Qt Code:
    1. tableModel.select();
    2. for( int i = 0 ; i < n; ++i ) {
    3. ...
    4. }
    5. tableModel.submitAll();
    To copy to clipboard, switch view to plain text mode 
    or
    Qt Code:
    1. tableModel.select();
    2. for( int i = 0 ; i < n; ++i ) {
    3. ...
    4. tableModel.submitAll();
    5. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. insertion of multiple items in a single row
    By grimbaum in forum Qt Programming
    Replies: 4
    Last Post: 17th November 2006, 05:36
  2. how to corss compile for windows in Linux
    By safknw in forum Qt Programming
    Replies: 24
    Last Post: 13th May 2006, 05:23

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.