Trolltech Home | Qt-interest Home | Recent Threads | All Threads | Author | Date
All threads index page 4

Qt-interest Archive, July 2004
Retrieving null database query values


Message 1 in thread

After over a year of blissful Qt database development, I am suddenly 
having so much trouble that I ripped out my entire Qt installation and 
reinstalled thinking it must somehow have corrupted.  The only thing I 
can think that has changed is a recent upgrade to KDE 3.2.3.

My current application makes extensive use of left joins.  I have always 
checked for null values with something like:

if ( query.value(0).toString().isNull() ) {

Suddenly, in one spot in my application, this seems to have stopped 
working.  I stop the application in a debugger right before the query is 
made, dump the query string and run it manually against the database and 
it returns a null value exactly as expected.  The application doesn't 
detect the null value.

I then noticed that there is a method, QVariant::isNull() which I had 
not noticed before.  I thought, perhaps, that is what I should use 
instead but that did not work either.

I then returned the value of query.value(0).toString() (which is 
supposed to QString::null or so I believe) in a QMessageBox for kicks 
and to my great surprise it returned the value "0".

Has something changed in Qt? Am I doing something incorrectly? How does 
one properly check for null values?

I am currently working around it by using toUInt() and checking for a 
zero returned but something seems seriously awry.  Thanks - John
-- 
 [ signature omitted ] 

Message 2 in thread

Yes, there is something weird with QString and null strings. I am on the track 
of this a long time ago, but I couldn't get it. Look at this thread where I 
expose a similar problem to yours:

http://lists.trolltech.com/qt-interest/2004-06/thread00443-0.html

The correct way to compare if a value is null is with

query.isNull(0)


QString s = ( query.isNull(0) ? QString() : query.value(0) );


Hope this helps!




On Monday 12 July 2004 11:43, John A. Sullivan III wrote:
> After over a year of blissful Qt database development, I am suddenly
> having so much trouble that I ripped out my entire Qt installation and
> reinstalled thinking it must somehow have corrupted.  The only thing I
> can think that has changed is a recent upgrade to KDE 3.2.3.
>
> My current application makes extensive use of left joins.  I have always
> checked for null values with something like:
>
> if ( query.value(0).toString().isNull() ) {
>
> Suddenly, in one spot in my application, this seems to have stopped
> working.  I stop the application in a debugger right before the query is
> made, dump the query string and run it manually against the database and
> it returns a null value exactly as expected.  The application doesn't
> detect the null value.
>
> I then noticed that there is a method, QVariant::isNull() which I had
> not noticed before.  I thought, perhaps, that is what I should use
> instead but that did not work either.
>
> I then returned the value of query.value(0).toString() (which is
> supposed to QString::null or so I believe) in a QMessageBox for kicks
> and to my great surprise it returned the value "0".
>
> Has something changed in Qt? Am I doing something incorrectly? How does
> one properly check for null values?
>
> I am currently working around it by using toUInt() and checking for a
> zero returned but something seems seriously awry.  Thanks - John

-- 
 [ signature omitted ] 

Message 3 in thread

Santiago Capel wrote:
> Yes, there is something weird with QString and null strings. I am on the track 
> of this a long time ago, but I couldn't get it. Look at this thread where I 
> expose a similar problem to yours:
> 
> http://lists.trolltech.com/qt-interest/2004-06/thread00443-0.html
> 
> The correct way to compare if a value is null is with
> 
> query.isNull(0)
> 
> 
> QString s = ( query.isNull(0) ? QString() : query.value(0) );
> 
> 
> Hope this helps!
><snip>
Yes, thank you very much.  However, I am a little concerned by the 
warning in the documentation:

"Note that for some drivers isNull() will not return accurate 
information until after an attempt is made to retrieve data"

What does that mean? Is this safe if all I am testing for is the null value?

By the way, how does one cut and paste out of the Qt documentation? I am 
viewing it under KDevelop.

Thanks, all - John
-- 
 [ signature omitted ]