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:
Code:
tableModel->setTable(tableA);
tableModel->select();
for(int i=0, i<rec.count (), 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!
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.
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).
Re: Question about method named record() in QSqlTableModel
Quote:
Originally Posted by
ChrisW67
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.
Re: Question about method named record() in QSqlTableModel
Quote:
Originally Posted by
wysota
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ï¼
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.
Re: Question about method named record() in QSqlTableModel
I would have determined the columns in the table thus:
Code:
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>". 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.
Re: Question about method named record() in QSqlTableModel
thank you all the same!
Added after 6 minutes:
Quote:
Originally Posted by
ChrisW67
I would have determined the columns in the table thus:
Code:
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>". 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!