[Qt-interest] QSqlQuery and dotted column names

Scott Aron Bloom Scott.Bloom at onshorecs.com
Sat Oct 30 19:41:47 CEST 2010


Depending on the SQL driver, the column names returned may only return
the string to the right of the dot.

Most likely query.record().indexOf( "firstname" ) works fine.

Scott

-----Original Message-----
From: qt-interest-bounces at trolltech.com
[mailto:qt-interest-bounces at trolltech.com] On Behalf Of TP
Sent: Friday, October 29, 2010 3:33 PM
To: qt-interest at trolltech.com
Subject: [Qt-interest] QSqlQuery and dotted column names

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)

_______________________________________________
Qt-interest mailing list
Qt-interest at trolltech.com
http://lists.trolltech.com/mailman/listinfo/qt-interest




More information about the Qt-interest-old mailing list