Results 1 to 5 of 5

Thread: Getting an Auto Increment value from QSqlTableModel with OnManualSubmit

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Aug 2007
    Posts
    5
    Qt products
    Qt4
    Platforms
    Unix/X11
    Thanked 1 Time in 1 Post

    Default Re: Getting an Auto Increment value from QSqlTableModel with OnManualSubmit

    submitAll() changes the underlying database and QSqlTableModel needs to requery for data correctness, submitAll() is used to submit changes , you could need to delete a record and submitAll() will save the changes on the database. Your problem is here, the second query breaks you plans. It is difficult to get the same table after submitAll() if you set a filter is even worst.
    If your intention is to display and insert a single row at once try this:
    after the submitAll() use QSqlDatabase::lastInsertId() to get the ID and call model->setFilter( myid is <QSqlDatabase::lastInsertId()> ) to get again the last inserted record.
    This should work only if you inserts one row at once, multiple rows will inserts multiple ids and you can get only the last or the first one I don't remember now.

    Daniele

  2. The following user says thank you to trueneo for this useful post:

    darkadept (10th April 2008)

  3. #2
    Join Date
    May 2006
    Posts
    70
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    12
    Thanked 4 Times in 2 Posts

    Default Re: Getting an Auto Increment value from QSqlTableModel with OnManualSubmit

    I haven't tried it out yet but that looks like the answer I'm looking for.

    Wysota, I haven't completely confirmed this but passing NULL to an auto increment works great with MySql but I was having trouble with it when using ODBC and MSSQL on Windows.

    I'll be testing out that NULL problem soon and post the results here but I have been little tied up as my wife just gave birth to our first child.

  4. #3
    Join Date
    May 2006
    Posts
    70
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    12
    Thanked 4 Times in 2 Posts

    Default Re: Getting an Auto Increment value from QSqlTableModel with OnManualSubmit

    I "accidentally" continued this post on a different thread. My apologies.

    Click here for that thread.

    @wysota passing NULL as the auto-increment value is the correct way of doing it in both MySQL and ODBC, thanks.

    @trueneo: what you suggested doesn't work for 2 reasons. First the QODBC driver does not support lastInsertId and secondly QSqlDatabase does not have a lastInsertId method.

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
  •  
Qt is a trademark of The Qt Company.