PDA

View Full Version : Question about method named record() in QSqlTableModel



silentyears
18th January 2014, 04:14
Hi all,
when i want to get the fields of one table in database, i use this code as follows:


QSqlTableModel tableModel;
tableModel->setTable(tableA);
tableModel->select();
QSqlRecord rec = tableModel->record();
for(int i=0, i<rec.count (), i++)
{
QSqlField field = rec.field(i);
QString fieldName = field.fieldName();
qDebug()<<fieldName;
// other
// ....
}

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!

wysota
18th January 2014, 10:43
I don't know the reason behind it, but this behavior is documented so it is not a bug.

ChrisW67
19th January 2014, 04:53
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).

wysota
19th January 2014, 09:17
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.

silentyears
19th January 2014, 17:36
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!

wysota
20th January 2014, 11:28
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.

ChrisW67
21st January 2014, 00:24
I would have determined the columns in the table thus:


QSqlDatabase db = QSqlDatabase::database();
QSqlRecord record = db.record("tablename");
for (int col = 0; col < record.count(); ++col)
qDebug() << col << record.fieldName(col); // or use QSqlField if you need types etc

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>" (http://www.contrib.andrew.cmu.edu/~shadow/sql/sql1992.txt). 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, 10:59
thank you all the same!

Added after 6 minutes:


I would have determined the columns in the table thus:


QSqlDatabase db = QSqlDatabase::database();
QSqlRecord record = db.record("tablename");
for (int col = 0; col < record.count(); ++col)
qDebug() << col << record.fieldName(col); // or use QSqlField if you need types etc

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>" (http://www.contrib.andrew.cmu.edu/~shadow/sql/sql1992.txt). 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!