I don't know the reason behind it, but this behavior is documented so it is not a bug.
I don't know the reason behind it, but this behavior is documented so it is not a bug.
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.
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.
silentyears (21st January 2014)
I would have determined the columns in the table thus:
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.Qt Code:
for (int col = 0; col < record.count(); ++col) qDebug() << col << record.fieldName(col); // or use QSqlField if you need types etcTo copy to clipboard, switch view to plain text mode
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.
silentyears (21st January 2014)
thank you all the same!
Added after 6 minutes:
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.
Bookmarks