Results 1 to 8 of 8

Thread: Question about method named record() in QSqlTableModel

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #7
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Wiki edits
    17

    Default Re: Question about method named record() in QSqlTableModel

    I would have determined the columns in the table thus:
    Qt Code:
    1. QSqlDatabase db = QSqlDatabase::database();
    2. QSqlRecord record = db.record("tablename");
    3. for (int col = 0; col < record.count(); ++col)
    4. qDebug() << col << record.fieldName(col); // or use QSqlField if you need types etc
    To copy to clipboard, switch view to plain text mode 
    I would expect Qt to simply report precisely the order the database engine gives it the columns. However, I read in QSqlDatabase::record() that this should not be relied on. The code I see for the Mysql driver should preserve order; perhaps others are different.

    On the database end each column has an ordinal position within its table (from the user perspective) and a "SELECT * FROM T" query should return the columns from the table in "the ascending sequence of their ordinal position within T" to be standards compliant. See ANSI SQL-92 "7.9 <query specification>". Perhaps you could try a QSqlQuery with this query to see if you get a different ordering. There might be some non-compliance/quirks on RDBMS systems that permit "ALTER TABLE T ADD COLUMN blah" at arbitrary positions in the column order.

    In general, you should not write code that depends on implied column order or assumes columns do not change if at all possible. Always name all columns in SELECT and INSERT and you will be glad when you have to maintain the code later.

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

    silentyears (21st January 2014)

Similar Threads

  1. Replies: 1
    Last Post: 12th April 2013, 19:46
  2. Record and row in a QSqlTableModel
    By QFreeCamellia in forum Newbie
    Replies: 8
    Last Post: 21st December 2011, 23:29
  3. Error Modifiying a record of a QSqlTableModel
    By qlands in forum Qt Programming
    Replies: 2
    Last Post: 28th July 2011, 10:41
  4. Replies: 1
    Last Post: 4th October 2010, 00:46
  5. deleting record (row) from QSqlTableModel
    By schnitzel in forum Qt Programming
    Replies: 3
    Last Post: 13th February 2010, 21:48

Tags for this Thread

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.