Results 1 to 8 of 8

Thread: Question about method named record() in QSqlTableModel

  1. #1
    Join Date
    Jan 2014
    Posts
    20
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Question about method named record() in QSqlTableModel

    Hi all,
    when i want to get the fields of one table in database, i use this code as follows:
    Qt Code:
    1. QSqlTableModel tableModel;
    2. tableModel->setTable(tableA);
    3. tableModel->select();
    4. QSqlRecord rec = tableModel->record();
    5. for(int i=0, i<rec.count (), i++)
    6. {
    7. QSqlField field = rec.field(i);
    8. QString fieldName = field.fieldName();
    9. qDebug()<<fieldName;
    10. // other
    11. // ....
    12. }
    To copy to clipboard, switch view to plain text mode 
    here is my question, when i frequently open multiple talbes like this, sometimes i find that qDebug the fields sequence of table is not as the same as the table in database(e.g Oracle) , so anyone knows the reseason?
    Thank you very much!

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,363
    Thanks
    3
    Thanked 5,012 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

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

    I don't know the reason behind it, but this behavior is documented so it is not a bug.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,546
    Thanks
    13
    Thanked 1,578 Times in 1,507 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

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

    Where is this documented? Cannot say I have seen it stated that QSqlRecord field order is arbitrary (or that it is not for that matter).

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,363
    Thanks
    3
    Thanked 5,012 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

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

    Quote Originally Posted by ChrisW67 View Post
    Where is this documented? Cannot say I have seen it stated that QSqlRecord field order is arbitrary (or that it is not for that matter).
    I don't know but my recently my friend told me that it is documented that if you do select * from table then the order of fields in the result is not specified. That might not be something Qt specific. As far as I remember, with the databases I worked with, the order was always like in the database table so here I'm just repeating something I had heard.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Jan 2014
    Posts
    20
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

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

    Quote Originally Posted by wysota View Post
    I don't know but my recently my friend told me that it is documented that if you do select * from table then the order of fields in the result is not specified. That might not be something Qt specific. As far as I remember, with the databases I worked with, the order was always like in the database table so here I'm just repeating something I had heard.
    Thank you very much! this thing make me crazy that I waste so much time to debug it ,if you can remember where did you read the statement,please tell me。
    And is there any idea to adjust the sequence!? thx again!

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,363
    Thanks
    3
    Thanked 5,012 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

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

    I did not read it anywhere. My friend told me he read it somewhere. Anyway, you can use QSqlField and QSqlRecord to discover the order of fields in a record.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. The following user says thank you to wysota for this useful post:

    silentyears (21st January 2014)

  8. #7
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,546
    Thanks
    13
    Thanked 1,578 Times in 1,507 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    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.

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

    silentyears (21st January 2014)

  10. #8
    Join Date
    Jan 2014
    Posts
    20
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

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

    thank you all the same!


    Added after 6 minutes:


    Quote Originally Posted by ChrisW67 View Post
    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.
    Thank you! you are so warm heart. I'll check it as you said, maybe it does because i use "Alert" to change the table. by the way, i use Qt5 and the database is oracle 11g, Maybe there is a way to keep the order unchanged while using "Alert" like Mysql ?
    Thx again!
    Last edited by silentyears; 21st January 2014 at 09:59.

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.