[Qt-interest] QSqlQuery and dotted column names

TP paratribulations at free.fr
Sat Oct 30 00:32:37 CEST 2010


Hi everybody,

I have a question about QSqlQuery and dotted column names defined with "AS" 
in a "SELECT" statement. See the simple example at the end of this post.
The problem is that below, we have -1 as a result for:

query.record().indexOf( "person.firstname" )

(we have defined a query with "AS 'person.firstname'" as alias for the 
"firstname" column of "person" table)

This means that the column name "person.firstname" is not found by 
.indexOf().
But if "person.firstname" is changed in "person_firstname" in both the 
SELECT statement and in .indexOf(), we obtain 0 which is the correct result.

What is the reason for that behavior? It seems to me that the SELECT 
statement with a dotted alias is perfectly legal from a SQLite point of 
view. So, why is it not found by indexOf()?

Thanks in advance,

Julien

PS: compilation on Linux with:
gcc -lQtSql -I/usr/include/qt4/QtCore -I/usr/include/qt4/QtSql -
I/usr/include/qt4 file.cpp

////////////////////////////////////////////
#include <QtSql>
#include <QDebug>

int main(int argc, char *argv[])
{
     QSqlDatabase db = QSqlDatabase::addDatabase( "QSQLITE" );

     db.setDatabaseName(":memory:");
     if (!db.open())
     {
       return 1;
     }

     QSqlQuery query;
     query.exec( "create table person ( firstname varchar(20), "
     "lastname varchar(20))" );
     query.exec( "insert into person values( 'Danny', 'Young')" );
     query.exec( "insert into person values( 'Christine', 'Holand')" );
     query.exec( "insert into person values( 'Lars', 'Gordon')" );
     
     query.exec("SELECT firstname"
     " AS 'person.firstname' FROM person");
     qDebug() << query.record().indexOf( "person.firstname" );
     while ( query.next() )
     {
       qDebug() << query.value(0).toString();
     }
}
////////////////////////////////////////////

-- 
python -c "print ''.join([chr(154 - ord(c)) for c in '*9(9&(18%.\
9&1+,\'Z4(55l4('])"

"When a distinguished but elderly scientist states that something is
possible, he is almost certainly right. When he states that something is
impossible, he is very probably wrong." (first law of AC Clarke)




More information about the Qt-interest-old mailing list