Results 1 to 2 of 2

Thread: new rows added using MySQL and QSqlTableModel aren't added at the end

  1. #1
    Join Date
    Sep 2010
    Fort Wayne, Indiana, USA
    Qt products

    Default new rows added using MySQL and QSqlTableModel aren't added at the end

    I am writting an application that displays a FUEL_MAPS table from a MySQL database and allows the user to enter additional rows of data using QTableView. During a software test, I added three new rows of data to the table.

    The three rows of data were entered correctly into the FUEL_MAPS table. However, when I run a sql query "SELECT * FROM FUEL_MAPS", the first row entered is at the beginning of the querry results. The second row entered is the last row of the querry results and the third row entered is the second entry. The newly entered rows are switching back and forth between the beginning of the query results and the end.

    I was expecting that the newly added rows would be at the end of the querry results. I would like to see the newly entered rows be the last entries in the FUEL_MAPS table.

    I am using the following software on Windows XP:
    1. MySQL 5.1
    2. Python 2.7
    3. Qt 4.6.2

    The FUEL_MAPS table has a column called ID which is auto-incrementing.

    The data entry screen has a "Add Row" button to add a new row. Here is the event handler code:

    #mdlFuelMaps is the QSqlTableModel
    row = self.mdlFuelMaps.rowCount()
    print "new code: row = %d" % row
    index = self.mdlFuelMaps.index(row, MAP_TYPE)
    #tblFuelMaps is the QTableView

    The row number is being incremented correctly.

    The data entry screen has a "submit" button to insert the data with this event handler code:

    submit button event handler
    def submitData(self) :

    if self.mdlFuelMaps.submitAll() :
    print "submitted the rows"
    else :
    print "error submitting the rows"

    When the submit button is clicked, I always get the print string "submitted the rows".

    Finally, I set the mdlFuelMaps edit property using the following statement when generating the entry screen:

    self.mdlFuelMaps.setEditStrategy(QSqlTableModel.On ManualSubmit)

    I've tried earlier versions of the code where I wasn't calling submitAll and didn't use the OnManualSubmit property but I got the same result. I also tried submitting each newly entered row.

    I feel the problem is one of these things:

    1. I need to call an additional function
    2. This is a "bug" with MySQL

    Has anybody else had this issue with MySQL? If you want to see all of code, let me know and I'll be glad to send it.

    Thank you.

  2. #2
    Join Date
    Mar 2009
    Brisbane, Australia
    Thanked 1,598 Times in 1,526 Posts
    Qt products
    Qt4 Qt5
    Unix/X11 Windows
    Wiki edits

    Default Re: new rows added using MySQL and QSqlTableModel aren't added at the end

    If you do not specify an ORDER BY clause in your selection SQL then the order that rows are returned from an RDBMS is not defined or guaranteed. Often the retrieval order is governed by the index or indexes used during query execution. For example, a hash-based index will give what appears a random ordering. Other reasons for seemingly random order are that new rows may be inserted into database pages in the space vacated by deleted rows.

    If you have an ID that is monotonically increasing then just ORDER BY that column (by using QSqlTableModel::setSort() in this case).

  3. The following user says thank you to ChrisW67 for this useful post:

    scot_hansen (29th October 2010)

Similar Threads

  1. Save added Data to a GUI
    By bostero22 in forum Qt Programming
    Replies: 1
    Last Post: 8th June 2010, 16:27
  2. Monitor a new added File
    By in forum Qt Programming
    Replies: 1
    Last Post: 27th February 2010, 10:50
  3. How can I added a button to QGrahpicsScene
    By learning_qt in forum Qt Programming
    Replies: 1
    Last Post: 21st September 2009, 09:51
  4. Replies: 3
    Last Post: 21st August 2009, 12:39
  5. Data not being added to my QTableView?
    By steg90 in forum Qt Programming
    Replies: 1
    Last Post: 20th November 2007, 15:20


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.